Fix integer division and refactor if statement
@@ -189,6 +189,7 @@ get_wg_index_range (kernel_run_command *k, unsigned *start_index,
const unsigned scaled_max_wgs = POCL_PTHREAD_MAX_WGS * num_threads;
const unsigned scaled_min_wgs = POCL_PTHREAD_MIN_WGS * num_threads;
+ unsigned limit;
unsigned max_wgs;
POCL_FAST_LOCK (k->lock);
if (k->remaining_wgs == 0)
@@ -205,10 +206,13 @@ get_wg_index_range (kernel_run_command *k, unsigned *start_index,
* num_threads, otherwise fallback to smaller workgroups.
*/
if (k->remaining_wgs <= (scaled_max_wgs * num_threads))
- max_wgs = min (scaled_min_wgs, (1 + k->remaining_wgs / num_threads));
+ limit = scaled_min_wgs;
else
- max_wgs = min (scaled_max_wgs, (1 + k->remaining_wgs / num_threads));
+ limit = scaled_max_wgs;
+ // divide two integers rounding up, i.e. ceil(k->remaining_wgs/num_threads)
+ const unsigned wgs_per_thread = (1 + (k->remaining_wgs - 1) / num_threads);
+ max_wgs = min (limit, wgs_per_thread);
max_wgs = min (max_wgs, k->remaining_wgs);
assert (max_wgs > 0);
