Skip to content

Commit caac9b4

Browse files
committed
The first argument of the max could still underflow so make these float type then cast to the smallest uint.
1 parent b403e69 commit caac9b4

2 files changed

Lines changed: 36 additions & 40 deletions

File tree

‎graph_framework/piecewise.hpp‎

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,14 @@ void compile_index(std::ostringstream &stream,
2828
const size_t length,
2929
const T scale,
3030
const T offset) {
31-
const std::string type = jit::smallest_int_type<T> (length);
32-
stream << "min";
31+
const std::string type = jit::type_to_string<T> ();
32+
stream << "(" << jit::smallest_uint_type<T> (length) << ")min";
3333
if constexpr (!jit::use_metal<T> ()) {
3434
stream << "<" << type << ">";
3535
}
36-
stream << "(";
37-
if constexpr (jit::use_metal<T> ()) {
38-
stream << "(" << type << ")";
39-
}
40-
stream << "max";
36+
stream << "(max";
4137
if constexpr (!jit::use_metal<T> ()) {
42-
stream << "<" << jit::type_to_string<T> () << ">";
38+
stream << "<" << type << ">";
4339
}
4440
stream << "(";
4541
if constexpr (jit::complex_scalar<T>) {
@@ -59,7 +55,7 @@ void compile_index(std::ostringstream &stream,
5955
}
6056
stream << ",";
6157
if constexpr (jit::use_metal<T> ()) {
62-
stream << "(" << jit::get_type_string<T> () << ")";
58+
stream << "(" << type << ")";
6359
}
6460
stream << "0),";
6561
if constexpr (jit::use_metal<T> ()) {
@@ -210,12 +206,12 @@ void compile_index(std::ostringstream &stream,
210206
if (constant_cast(this->arg).get()) {
211207
const T arg = (this->arg->evaluate().at(0) + offset)/scale;
212208
if constexpr (jit::float_base<T>) {
213-
const size_t i = std::max<size_t> (std::min<float> (std::real(arg),
214-
this->get_size() - 1),
215-
0);
209+
const size_t i = std::max<float> (std::min<float> (std::real(arg),
210+
this->get_size() - 1),
211+
0);
216212
return constant<T, SAFE_MATH> (leaf_node<T, SAFE_MATH>::caches.backends[data_hash][i]);
217213
} else {
218-
const size_t i = std::max<size_t> (std::min<double> (std::real(arg),
214+
const size_t i = std::max<double> (std::min<double> (std::real(arg),
219215
this->get_size() - 1),
220216
0);
221217
return constant<T, SAFE_MATH> (leaf_node<T, SAFE_MATH>::caches.backends[data_hash][i]);
@@ -358,7 +354,7 @@ void compile_index(std::ostringstream &stream,
358354
#ifdef USE_INDEX_CACHE
359355
indices[a.get()] = jit::to_string('i', a.get());
360356
stream << " const "
361-
<< jit::smallest_int_type<T> (length) << " "
357+
<< jit::smallest_uint_type<T> (length) << " "
362358
<< indices[a.get()] << " = ";
363359
compile_index<T> (stream, registers[a.get()], length,
364360
scale, offset);
@@ -862,18 +858,18 @@ void compile_index(std::ostringstream &stream,
862858
const T r = (this->right->evaluate().at(0) + y_offset)/y_scale;
863859

864860
if constexpr (jit::float_base<T>) {
865-
const size_t i = std::max<size_t> (std::min<float> (std::real(l),
866-
this->get_num_rows() - 1),
867-
0);
868-
const size_t j = std::max<size_t> (std::min<float> (std::real(r),
869-
this->get_num_columns() - 1),
870-
0);
861+
const size_t i = std::max<float> (std::min<float> (std::real(l),
862+
this->get_num_rows() - 1),
863+
0);
864+
const size_t j = std::max<float> (std::min<float> (std::real(r),
865+
this->get_num_columns() - 1),
866+
0);
871867
return constant<T, SAFE_MATH> (leaf_node<T, SAFE_MATH>::caches.backends[data_hash][i*this->get_num_columns() + j]);
872868
} else {
873-
const size_t i = std::max<size_t> (std::min<double> (std::real(l),
869+
const size_t i = std::max<double> (std::min<double> (std::real(l),
874870
this->get_num_rows() - 1),
875871
0);
876-
const size_t j = std::max<size_t> (std::min<double> (std::real(r),
872+
const size_t j = std::max<double> (std::min<double> (std::real(r),
877873
this->get_num_columns() - 1),
878874
0);
879875
return constant<T, SAFE_MATH> (leaf_node<T, SAFE_MATH>::caches.backends[data_hash][i*this->get_num_columns() + j]);
@@ -882,13 +878,13 @@ void compile_index(std::ostringstream &stream,
882878
const T l = (this->left->evaluate().at(0) + x_offset)/x_scale;
883879

884880
if constexpr (jit::float_base<T>) {
885-
const size_t i = std::max<size_t> (std::min<float> (std::real(l),
886-
this->get_num_rows() - 1),
887-
0);
881+
const size_t i = std::max<float> (std::min<float> (std::real(l),
882+
this->get_num_rows() - 1),
883+
0);
888884
return piecewise_1D(leaf_node<T, SAFE_MATH>::caches.backends[data_hash].index_row(i, this->get_num_columns()),
889885
this->right, y_scale, y_offset);
890886
} else {
891-
const size_t i = std::max<size_t> (std::min<double> (std::real(l),
887+
const size_t i = std::max<double> (std::min<double> (std::real(l),
892888
this->get_num_rows() - 1),
893889
0);
894890
return piecewise_1D(leaf_node<T, SAFE_MATH>::caches.backends[data_hash].index_row(i, this->get_num_columns()),
@@ -898,13 +894,13 @@ void compile_index(std::ostringstream &stream,
898894
const T r = (this->right->evaluate().at(0) + y_offset)/y_scale;
899895

900896
if constexpr (jit::float_base<T>) {
901-
const size_t j = std::max<size_t> (std::min<float> (std::real(r),
902-
this->get_num_columns() - 1),
903-
0);
897+
const size_t j = std::max<float> (std::min<float> (std::real(r),
898+
this->get_num_columns() - 1),
899+
0);
904900
return piecewise_1D(leaf_node<T, SAFE_MATH>::caches.backends[data_hash].index_column(j, this->get_num_columns()),
905901
this->left, x_scale, x_offset);
906902
} else {
907-
const size_t j = std::max<size_t> (std::min<double> (std::real(r),
903+
const size_t j = std::max<double> (std::min<double> (std::real(r),
908904
this->get_num_columns() - 1),
909905
0);
910906
return piecewise_1D(leaf_node<T, SAFE_MATH>::caches.backends[data_hash].index_column(j, this->get_num_columns()),
@@ -1071,7 +1067,7 @@ void compile_index(std::ostringstream &stream,
10711067
if (indices.find(x.get()) == indices.end()) {
10721068
indices[x.get()] = jit::to_string('i', x.get());
10731069
stream << " const "
1074-
<< jit::smallest_int_type<T> (num_rows) << " "
1070+
<< jit::smallest_uint_type<T> (num_rows) << " "
10751071
<< indices[x.get()] << " = ";
10761072
compile_index<T> (stream, registers[x.get()], num_rows,
10771073
x_scale, x_offset);
@@ -1080,7 +1076,7 @@ void compile_index(std::ostringstream &stream,
10801076
if (indices.find(y.get()) == indices.end()) {
10811077
indices[y.get()] = jit::to_string('i', y.get());
10821078
stream << " const "
1083-
<< jit::smallest_int_type<T> (num_columns) << " "
1079+
<< jit::smallest_uint_type<T> (num_columns) << " "
10841080
<< indices[y.get()] << " = ";
10851081
compile_index<T> (stream, registers[y.get()], num_columns,
10861082
y_scale, y_offset);
@@ -1096,7 +1092,7 @@ void compile_index(std::ostringstream &stream,
10961092
if (indices.find(temp.get()) == indices.end()) {
10971093
indices[temp.get()] = jit::to_string('i', temp.get());
10981094
stream << " const "
1099-
<< jit::smallest_int_type<T> (length) << " "
1095+
<< jit::smallest_uint_type<T> (length) << " "
11001096
<< indices[temp.get()] << " = "
11011097
<< indices[x.get()]
11021098
<< "*" << num_columns << " + "
@@ -1131,8 +1127,8 @@ void compile_index(std::ostringstream &stream,
11311127
if constexpr (jit::use_metal<T> ()) {
11321128
#ifdef USE_INDEX_CACHE
11331129
stream << ".read("
1134-
<< jit::smallest_int_type<T> (std::max(num_rows,
1135-
num_columns))
1130+
<< jit::smallest_uint_type<T> (std::max(num_rows,
1131+
num_columns))
11361132
<< "2("
11371133
<< indices[y.get()]
11381134
<< ","
@@ -1517,7 +1513,7 @@ void compile_index(std::ostringstream &stream,
15171513
#ifdef USE_INDEX_CACHE
15181514
indices[a.get()] = jit::to_string('i', a.get());
15191515
stream << " const "
1520-
<< jit::smallest_int_type<T> (length) << " "
1516+
<< jit::smallest_uint_type<T> (length) << " "
15211517
<< indices[a.get()] << " = ";
15221518
compile_index<T> (stream, registers[a.get()], length,
15231519
scale, offset);
@@ -1853,7 +1849,7 @@ void compile_index(std::ostringstream &stream,
18531849
if (indices.find(x.get()) == indices.end()) {
18541850
indices[x.get()] = jit::to_string('i', x.get());
18551851
stream << " const "
1856-
<< jit::smallest_int_type<T> (num_rows) << " "
1852+
<< jit::smallest_uint_type<T> (num_rows) << " "
18571853
<< indices[x.get()] << " = ";
18581854
compile_index<T> (stream, registers[x.get()], num_rows,
18591855
x_scale, x_offset);
@@ -1862,7 +1858,7 @@ void compile_index(std::ostringstream &stream,
18621858
if (indices.find(y.get()) == indices.end()) {
18631859
indices[y.get()] = jit::to_string('i', y.get());
18641860
stream << " const "
1865-
<< jit::smallest_int_type<T> (num_columns) << " "
1861+
<< jit::smallest_uint_type<T> (num_columns) << " "
18661862
<< indices[y.get()] << " = ";
18671863
compile_index<T> (stream, registers[y.get()], num_columns,
18681864
y_scale, y_offset);
@@ -1875,7 +1871,7 @@ void compile_index(std::ostringstream &stream,
18751871
if (indices.find(temp.get()) == indices.end()) {
18761872
indices[temp.get()] = jit::to_string('i', temp.get());
18771873
stream << " const "
1878-
<< jit::smallest_int_type<T> (length) << " "
1874+
<< jit::smallest_uint_type<T> (length) << " "
18791875
<< indices[temp.get()] << " = "
18801876
<< indices[x.get()]
18811877
<< "*" << num_columns << " + "

‎graph_framework/register.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ namespace jit {
105105
/// @returns The smallest integer type as a string.
106106
//------------------------------------------------------------------------------
107107
template<float_scalar T>
108-
std::string smallest_int_type(const size_t max_size) {
108+
std::string smallest_uint_type(const size_t max_size) {
109109
if (max_size <= std::numeric_limits<unsigned char>::max()) {
110110
if constexpr (jit::use_metal<T> ()) {
111111
return "ushort";

0 commit comments

Comments
 (0)