Skip to content

Commit 425e3be

Browse files
committed
as_signed and as_unsigned
1 parent 350ff1f commit 425e3be

13 files changed

Lines changed: 106 additions & 76 deletions

File tree

include/xtensor/generators/xbuilder.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "../core/xoperation.hpp"
3131
#include "../generators/xgenerator.hpp"
3232
#include "../views/xbroadcast.hpp"
33+
#include "../utils/xutils.hpp"
34+
3335

3436
namespace xt
3537
{
@@ -498,8 +500,8 @@ namespace xt
498500
inline value_type access(const tuple_type& t, size_type axis, It first, It last) const
499501
{
500502
// trim off extra indices if provided to match behavior of containers
501-
auto dim_offset = std::distance(first, last) - std::get<0>(t).dimension();
502-
size_t axis_dim = *(first + axis + dim_offset);
503+
auto dim_offset = std::distance(first, last) - as_signed(std::get<0>(t).dimension());
504+
size_t axis_dim = as_unsigned(*(first + as_signed(axis) + dim_offset));
503505
auto match = [&](auto& arr)
504506
{
505507
if (axis_dim >= arr.shape()[axis])
@@ -520,7 +522,7 @@ namespace xt
520522
const size_t stride = std::accumulate(
521523
shape.begin() + i + 1,
522524
shape.end(),
523-
1,
525+
1ul,
524526
std::multiplies<size_t>()
525527
);
526528
if (i == axis)
@@ -529,11 +531,11 @@ namespace xt
529531
}
530532
else
531533
{
532-
const auto len = (*(first + i + dim_offset));
534+
const auto len = as_unsigned(*(first + as_signed(i) + dim_offset));
533535
offset += len * stride;
534536
}
535537
}
536-
const auto element = arr.begin() + offset;
538+
const auto element = arr.begin() + as_signed(offset);
537539
return *element;
538540
};
539541

@@ -563,7 +565,7 @@ namespace xt
563565
{
564566
auto get_item = [&](auto& arr)
565567
{
566-
size_t offset = 0;
568+
std::ptrdiff_t offset = 0;
567569
const size_t end = arr.dimension();
568570
size_t after_axis = 0;
569571
for (size_t i = 0; i < end; i++)
@@ -576,16 +578,16 @@ namespace xt
576578
const size_t stride = std::accumulate(
577579
shape.begin() + i + 1,
578580
shape.end(),
579-
1,
581+
1ul,
580582
std::multiplies<size_t>()
581583
);
582-
const auto len = (*(first + i + after_axis));
583-
offset += len * stride;
584+
const auto len = as_unsigned(*(first + as_signed(i + after_axis)));
585+
offset += as_signed(len * stride);
584586
}
585587
const auto element = arr.begin() + offset;
586588
return *element;
587589
};
588-
size_type i = *(first + axis);
590+
auto i = as_unsigned((*(first + as_signed(axis))));
589591
return apply<value_type>(i, get_item, t);
590592
}
591593
};

include/xtensor/misc/xfft.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "../views/xaxis_slice_iterator.hpp"
1414
#include "../views/xview.hpp"
1515
#include "./xtl_concepts.hpp"
16+
#include "../utils/xutils.hpp"
1617

1718
namespace xt
1819
{
@@ -61,15 +62,15 @@ namespace xt
6162
auto odd = radix2(xt::view(ev, xt::range(1, _, 2)));
6263
#endif
6364

64-
auto range = xt::arange<double>(N / 2);
65-
auto exp = xt::exp(static_cast<value_type>(-2i) * pi * range / N);
65+
auto range = xt::arange<double>(0.5*static_cast<double>(N));
66+
auto exp = xt::exp(static_cast<value_type>(-2i) * pi * range / static_cast<precision>(N));
6667
auto t = exp * odd;
6768
auto first_half = even + t;
6869
auto second_half = even - t;
6970
// TODO: should be a call to stack if performance was improved
7071
auto spectrum = xt::xtensor<value_type, 1>::from_shape({N});
71-
xt::view(spectrum, xt::range(0, N / 2)) = first_half;
72-
xt::view(spectrum, xt::range(N / 2, N)) = second_half;
72+
xt::view(spectrum, xt::range(0, static_cast<size_t>(N / 2))) = first_half;
73+
xt::view(spectrum, xt::range(static_cast<size_t>(N / 2), N)) = second_half;
7374
return spectrum;
7475
}
7576
}
@@ -82,15 +83,15 @@ namespace xt
8283

8384
// Find a power-of-2 convolution length m such that m >= n * 2 + 1
8485
const std::size_t n = data.size();
85-
size_t m = std::ceil(std::log2(n * 2 + 1));
86-
m = std::pow(2, m);
86+
auto m = static_cast<size_t>(std::ceil(std::log2(n * 2 + 1)));
87+
m = 1 << m;
8788

8889
// Trignometric table
8990
auto exp_table = xt::xtensor<std::complex<precision>, 1>::from_shape({n});
9091
xt::xtensor<std::size_t, 1> i = xt::pow(xt::linspace<std::size_t>(0, n - 1, n), 2);
9192
i %= (n * 2);
9293

93-
auto angles = xt::eval(precision{3.141592653589793238463} * i / n);
94+
auto angles = xt::eval(static_cast<precision>(3.141592653589793238463) * xt::cast<precision>(i) / static_cast<precision>(n));
9495
auto j = std::complex<precision>(0, 1);
9596
exp_table = xt::exp(-angles * j);
9697

@@ -112,7 +113,7 @@ namespace xt
112113
auto spectrum_k = xv * yv;
113114
auto complex_args = xt::conj(spectrum_k);
114115
auto fft_res = radix2(complex_args);
115-
auto cv = xt::conj(fft_res) / m;
116+
auto cv = xt::conj(fft_res) / static_cast<precision>(m);
116117

117118
return xt::eval(xt::view(cv, xt::range(0, n)) * exp_table);
118119
}
@@ -162,7 +163,7 @@ namespace xt
162163
if constexpr (xtl::is_complex<typename std::decay<E>::type::value_type>::value)
163164
{
164165
// check the length of the data on that axis
165-
const std::size_t n = e.shape(axis);
166+
const std::size_t n = e.shape(as_unsigned(axis));
166167
if (n == 0)
167168
{
168169
XTENSOR_THROW(std::runtime_error, "Cannot take the iFFT along an empty dimention");
@@ -218,7 +219,7 @@ namespace xt
218219
auto outvec = ifft(xv, axis);
219220

220221
// Scaling (because this FFT implementation omits it)
221-
outvec = outvec / n;
222+
outvec = outvec / static_cast<double>(n);
222223

223224
return outvec;
224225
}

include/xtensor/misc/xmanipulation.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ namespace xt
296296
auto perm = xtl::make_sequence<S>(dim, 0);
297297
using id_t = typename S::value_type;
298298
std::iota(perm.begin(), perm.end(), id_t(0));
299-
perm[ax1] = ax2;
300-
perm[ax2] = ax1;
299+
perm[ax1] = static_cast<id_t>(ax2);
300+
perm[ax2] = static_cast<id_t>(ax1);
301301
return perm;
302302
}
303303
}
@@ -339,18 +339,18 @@ namespace xt
339339

340340
// Initializing to src_norm handles case where `dest == -1` and the loop
341341
// does not go check `perm_idx == dest_norm` a `dim+1`th time.
342-
auto perm = xtl::make_sequence<S>(dim, src_norm);
342+
auto perm = xtl::make_sequence<S>(dim, static_cast<id_t>(src_norm));
343343
id_t perm_idx = 0;
344344
for (id_t i = 0; xtl::cmp_less(i, dim); ++i)
345345
{
346-
if (xtl::cmp_equal(perm_idx, dest_norm))
346+
if (xtl::cmp_equal(static_cast<std::size_t>(perm_idx), dest_norm))
347347
{
348-
perm[perm_idx] = src_norm;
348+
perm[static_cast<std::size_t>(perm_idx)] = static_cast<id_t>(src_norm);
349349
++perm_idx;
350350
}
351-
if (xtl::cmp_not_equal(i, src_norm))
351+
if (xtl::cmp_not_equal(static_cast<std::size_t>(i), src_norm))
352352
{
353-
perm[perm_idx] = i;
353+
perm[static_cast<std::size_t>(perm_idx)] = i;
354354
++perm_idx;
355355
}
356356
}

include/xtensor/misc/xsort.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace xt
7777
{
7878
XTENSOR_ASSERT(ev.dimension() >= 2);
7979

80-
const std::size_t n_iters = leading_axis_n_iters(ev);
80+
const auto n_iters = as_signed(leading_axis_n_iters(ev));
8181
const std::ptrdiff_t secondary_stride = get_secondary_stride(ev);
8282

8383
const auto begin = ev.data();
@@ -94,7 +94,7 @@ namespace xt
9494
XTENSOR_ASSERT(e1.dimension() >= 2);
9595
XTENSOR_ASSERT(e1.dimension() == e2.dimension());
9696

97-
const std::size_t n_iters = leading_axis_n_iters(e1);
97+
const auto n_iters = as_signed(leading_axis_n_iters(e1));
9898
const std::ptrdiff_t secondary_stride1 = get_secondary_stride(e1);
9999
const std::ptrdiff_t secondary_stride2 = get_secondary_stride(e2);
100100
XTENSOR_ASSERT(secondary_stride1 == secondary_stride2);
@@ -294,7 +294,7 @@ namespace xt
294294
template <class ConstRandomIt, class RandomIt, class Compare, class Method>
295295
inline void argsort_iter(
296296
ConstRandomIt data_begin,
297-
ConstRandomIt data_end,
297+
[[maybe_unused]] ConstRandomIt data_end,
298298
RandomIt idx_begin,
299299
RandomIt idx_end,
300300
Compare comp,
@@ -315,7 +315,7 @@ namespace xt
315315
idx_end,
316316
[&](const auto i, const auto j)
317317
{
318-
return comp(*(data_begin + i), *(data_begin + j));
318+
return comp(*(data_begin + as_signed(i)), *(data_begin + as_signed(j)));
319319
}
320320
);
321321
}
@@ -326,7 +326,7 @@ namespace xt
326326
idx_end,
327327
[&](const auto i, const auto j)
328328
{
329-
return comp(*(data_begin + i), *(data_begin + j));
329+
return comp(*(data_begin + as_signed(i)), *(data_begin + as_signed(j)));
330330
}
331331
);
332332
}
@@ -784,7 +784,7 @@ namespace xt
784784
for (auto i : indices)
785785
{
786786
auto idx = current_index;
787-
idx[current_dim] = i;
787+
idx[current_dim] = as_signed(i);
788788
select_indices_impl(shape, indices, axis, current_dim + 1, idx, out);
789789
}
790790
}
@@ -793,7 +793,7 @@ namespace xt
793793
for (id_t i = 0; xtl::cmp_less(i, shape[current_dim]); ++i)
794794
{
795795
auto idx = current_index;
796-
idx[current_dim] = i;
796+
idx[current_dim] = as_signed(i);
797797
select_indices_impl(shape, indices, axis, current_dim + 1, idx, out);
798798
}
799799
}
@@ -802,7 +802,7 @@ namespace xt
802802
for (auto i : indices)
803803
{
804804
auto idx = current_index;
805-
idx[current_dim] = i;
805+
idx[current_dim] = as_signed(i);
806806
out.push_back(std::move(idx));
807807
}
808808
}
@@ -811,7 +811,7 @@ namespace xt
811811
for (id_t i = 0; xtl::cmp_less(i, shape[current_dim]); ++i)
812812
{
813813
auto idx = current_index;
814-
idx[current_dim] = i;
814+
idx[current_dim] = as_signed(i);
815815
out.push_back(std::move(idx));
816816
}
817817
}
@@ -834,7 +834,7 @@ namespace xt
834834
const std::size_t ax = normalize_axis(e.dimension(), axis);
835835
using shape_t = get_strides_t<typename std::decay_t<E>::shape_type>;
836836
auto shape = xtl::forward_sequence<shape_t, decltype(e.shape())>(e.shape());
837-
shape[ax] = indices.size();
837+
shape[ax] = as_signed(indices.size());
838838
return reshape_view(
839839
index_view(std::forward<E>(e), select_indices(e.shape(), indices, ax)),
840840
std::move(shape)
@@ -915,12 +915,12 @@ namespace xt
915915
auto kth_gamma = detail::quantile_kth_gamma<T, id_t, P>(n, probas, alpha, beta);
916916

917917
// Select relevant values for computing interpolating quantiles
918-
auto e_partition = xt::partition(std::forward<E>(e), kth_gamma.first, ax);
919-
auto e_kth = detail::fancy_indexing(std::move(e_partition), std::move(kth_gamma.first), ax);
918+
auto e_partition = xt::partition(std::forward<E>(e), kth_gamma.first, as_signed(ax));
919+
auto e_kth = detail::fancy_indexing(std::move(e_partition), std::move(kth_gamma.first), as_signed(ax));
920920

921921
// Reshape interpolation coefficients
922922
auto gm1_g_shape = xtl::make_sequence<tmp_shape_t>(e.dimension(), 1);
923-
gm1_g_shape[ax] = kth_gamma.second.size();
923+
gm1_g_shape[ax] = as_signed(kth_gamma.second.size());
924924
auto gm1_g_reshaped = reshape_view(std::move(kth_gamma.second), std::move(gm1_g_shape));
925925

926926
// Compute interpolation
@@ -930,9 +930,9 @@ namespace xt
930930
auto e_kth_g_shape = detail::unsqueeze_shape(e_kth_g.shape(), ax);
931931
e_kth_g_shape[ax] = 2;
932932
e_kth_g_shape[ax + 1] /= 2;
933-
auto quantiles = xt::sum(reshape_view(std::move(e_kth_g), std::move(e_kth_g_shape)), ax);
933+
auto quantiles = xt::sum(reshape_view(std::move(e_kth_g), std::move(e_kth_g_shape)), as_signed(ax));
934934
// Cannot do a transpose on a non-strided expression so we have to eval
935-
return moveaxis(eval(std::move(quantiles)), ax, 0);
935+
return moveaxis(eval(std::move(quantiles)), as_signed(ax), 0);
936936
}
937937

938938
// Static proba array overload

include/xtensor/utils/xutils.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,25 @@ namespace xt
10501050
template <class E, size_t N>
10511051
using has_rank_t = typename has_rank<std::decay_t<E>, N>::type;
10521052

1053+
/*************
1054+
* as_signed *
1055+
*************/
1056+
1057+
template <class T>
1058+
std::make_signed_t<T> as_signed(T t)
1059+
{
1060+
return static_cast<std::make_signed_t<T>>(t);
1061+
}
1062+
1063+
/***************
1064+
* as_unsigned *
1065+
***************/
1066+
1067+
template <class T>
1068+
std::make_unsigned_t<T> as_unsigned(T t)
1069+
{
1070+
return static_cast<std::make_unsigned_t<T>>(t);
1071+
}
10531072
}
10541073

10551074
#endif

include/xtensor/views/index_mapper.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define XTENSOR_INDEX_MAPPER_HPP
1212

1313
#include "xview.hpp"
14+
#include "../utils/xutils.hpp"
1415

1516
namespace xt
1617
{
@@ -400,8 +401,8 @@ namespace xt
400401
{
401402
constexpr size_t n_indices_full = n_indices_full_v<FirstIndice, OtherIndices...>;
402403

403-
constexpr size_t underlying_n_dimensions = xt::static_dimension<
404-
typename std::decay_t<UnderlyingContainer>::shape_type>::value;
404+
constexpr auto underlying_n_dimensions = as_unsigned(xt::static_dimension<
405+
typename std::decay_t<UnderlyingContainer>::shape_type>::value);
405406

406407
// If there is too many indices, we need to drop the first ones.
407408
// If the number of dimensions of the underlying container is known at compile time we can drop them
@@ -511,12 +512,12 @@ namespace xt
511512
if constexpr (std::is_integral_v<current_slice>)
512513
{
513514
assert(i == 0);
514-
return size_t(slice);
515+
return as_unsigned(slice);
515516
}
516517
else
517518
{
518519
assert(i < slice.size());
519-
return size_t(slice(i));
520+
return as_unsigned(slice(as_signed(i)));
520521
}
521522
}
522523
else

include/xtensor/views/xindex_view.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ namespace xt
511511
template <class It>
512512
inline auto xindex_view<CT, I>::element(It first, It /*last*/) -> reference
513513
{
514-
return m_e[m_indices[(*first)]];
514+
return m_e[m_indices[as_unsigned(*first)]];
515515
}
516516

517517
/**
@@ -523,7 +523,7 @@ namespace xt
523523
template <class It>
524524
inline auto xindex_view<CT, I>::element(It first, It /*last*/) const -> const_reference
525525
{
526-
return m_e[m_indices[(*first)]];
526+
return m_e[m_indices[as_unsigned(*first)]];
527527
}
528528

529529
/**

0 commit comments

Comments
 (0)