'Clamp the index in [0, size].'

def clamp_index(self, idx):

    builder = self._builder
    idxptr = cgutils.alloca_once_value(builder, idx)
    zero = ir.Constant(idx.type, 0)
    size = self.size
    underflow = self._builder.icmp_signed('<', idx, zero)
    with builder.if_then(underflow, likely=False):
        builder.store(zero, idxptr)
    overflow = self._builder.icmp_signed('>=', idx, size)
    with builder.if_then(overflow, likely=False):
        builder.store(size, idxptr)
    return builder.load(idxptr)
