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