#include "libgomp.h"
#include <stdlib.h>
int
gomp_iter_static_next (long *pstart, long *pend)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_team *team = thr->ts.team;
struct gomp_work_share *ws = thr->ts.work_share;
unsigned long nthreads = team ? team->nthreads : 1;
if (thr->ts.static_trip == -1)
return -1;
if (nthreads == 1)
{
*pstart = ws->next;
*pend = ws->end;
thr->ts.static_trip = -1;
return ws->next == ws->end;
}
if (ws->chunk_size == 0)
{
unsigned long n, q, i;
unsigned long s0, e0;
long s, e;
if (thr->ts.static_trip > 0)
return 1;
s = ws->incr + (ws->incr > 0 ? -1 : 1);
n = (ws->end - ws->next + s) / ws->incr;
i = thr->ts.team_id;
q = n / nthreads;
q += (q * nthreads != n);
s0 = q * i;
e0 = s0 + q;
if (e0 > n)
e0 = n;
if (s0 >= e0)
{
thr->ts.static_trip = 1;
return 1;
}
s = (long)s0 * ws->incr + ws->next;
e = (long)e0 * ws->incr + ws->next;
*pstart = s;
*pend = e;
thr->ts.static_trip = (e0 == n ? -1 : 1);
return 0;
}
else
{
unsigned long n, s0, e0, i, c;
long s, e;
s = ws->incr + (ws->incr > 0 ? -1 : 1);
n = (ws->end - ws->next + s) / ws->incr;
i = thr->ts.team_id;
c = ws->chunk_size;
s0 = (thr->ts.static_trip * nthreads + i) * c;
e0 = s0 + c;
if (s0 >= n)
return 1;
if (e0 > n)
e0 = n;
s = (long)s0 * ws->incr + ws->next;
e = (long)e0 * ws->incr + ws->next;
*pstart = s;
*pend = e;
if (e0 == n)
thr->ts.static_trip = -1;
else
thr->ts.static_trip++;
return 0;
}
}
bool
gomp_iter_dynamic_next_locked (long *pstart, long *pend)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_work_share *ws = thr->ts.work_share;
long start, end, chunk, left;
start = ws->next;
if (start == ws->end)
return false;
chunk = ws->chunk_size * ws->incr;
left = ws->end - start;
if (ws->incr < 0)
{
if (chunk < left)
chunk = left;
}
else
{
if (chunk > left)
chunk = left;
}
end = start + chunk;
ws->next = end;
*pstart = start;
*pend = end;
return true;
}
#ifdef HAVE_SYNC_BUILTINS
bool
gomp_iter_dynamic_next (long *pstart, long *pend)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_work_share *ws = thr->ts.work_share;
long start, end, nend, chunk, incr;
start = ws->next;
end = ws->end;
incr = ws->incr;
chunk = ws->chunk_size * incr;
while (1)
{
long left = end - start;
long tmp;
if (start == end)
return false;
if (incr < 0)
{
if (chunk < left)
chunk = left;
}
else
{
if (chunk > left)
chunk = left;
}
nend = start + chunk;
tmp = __sync_val_compare_and_swap (&ws->next, start, nend);
if (__builtin_expect (tmp == start, 1))
break;
start = tmp;
}
*pstart = start;
*pend = nend;
return true;
}
#endif
bool
gomp_iter_guided_next_locked (long *pstart, long *pend)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_work_share *ws = thr->ts.work_share;
struct gomp_team *team = thr->ts.team;
unsigned long nthreads = team ? team->nthreads : 1;
unsigned long n, q;
long start, end;
if (ws->next == ws->end)
return false;
n = (ws->end - ws->next) / ws->incr;
q = (n + nthreads - 1) / nthreads;
if (q < ws->chunk_size)
q = ws->chunk_size;
if (q > n)
q = n;
start = ws->next;
end = start + q * ws->incr;
ws->next = end;
*pstart = start;
*pend = end;
return true;
}
#ifdef HAVE_SYNC_BUILTINS
bool
gomp_iter_guided_next (long *pstart, long *pend)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_work_share *ws = thr->ts.work_share;
struct gomp_team *team = thr->ts.team;
unsigned long nthreads = team ? team->nthreads : 1;
long start, end, nend, incr;
unsigned long chunk_size;
start = ws->next;
end = ws->end;
incr = ws->incr;
chunk_size = ws->chunk_size;
while (1)
{
unsigned long n, q;
long tmp;
if (start == end)
return false;
n = (end - start) / ws->incr;
q = (n + nthreads - 1) / nthreads;
if (q < chunk_size)
q = chunk_size;
if (q > n)
q = n;
nend = start + q * incr;
tmp = __sync_val_compare_and_swap (&ws->next, start, nend);
if (__builtin_expect (tmp == start, 1))
break;
start = tmp;
}
*pstart = start;
*pend = nend;
return true;
}
#endif