2 * Use the fixed point version of Barrett reduction to compute a mod n
3 * over GF(2) for given n using POWER8 instructions. We use k = 32.
5 * http://en.wikipedia.org/wiki/Barrett_reduction
7 * Copyright (C) 2015 Anton Blanchard <anton@au.ibm.com>, IBM
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of either:
12 * a) the GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the License, or (at your option)
14 * any later version, or
15 * b) the Apache License, Version 2.0
18 #include "common/ppc-opcode.h"
33 .barrett_fz_constants:
34 /* Barrett constant m - (4^32)/n */
35 .octa 0x0000000000000000000000011f91caf6 /* x^64 div p(x) */
36 /* Barrett constant n */
37 .octa 0x0000000000000000000000011edc6f41
40 /* unsigned int barrett_reduction(unsigned long val) */
41 FUNC_START(barrett_reduction)
42 addis r4,r2,.barrett_fz_constants@toc@ha
43 addi r4,r4,.barrett_fz_constants@toc@l
46 vxor v1,v1,v1 /* zero v1 */
50 vsldoi v0,v1,v0,8 /* shift into bottom 64 bits, this is a */
57 * Now for the actual algorithm. The idea is to calculate q,
58 * the multiple of our polynomial that we need to subtract. By
59 * doing the computation 2x bits higher (ie 64 bits) and shifting the
60 * result back down 2x bits, we round down to the nearest multiple.
62 VPMSUMD(v4,v0,v2) /* ma */
63 vsldoi v4,v1,v4,8 /* q = floor(ma/(2^64)) */
64 VPMSUMD(v4,v4,v3) /* qn */
65 vxor v0,v0,v4 /* a - qn, subtraction is xor in GF(2) */
68 * Get the result into r3. We need to shift it left 8 bytes:
72 vsldoi v0,v0,v1,8 /* shift result into top 64 bits of v0 */
76 FUNC_END(barrett_reduction)