The two result arrays (*val and *pos) must be aligned to the source array, with replicated alignment in dimension dim. The mask array must be value-aligned with the source array (see section 4.5).