• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ppc64_helpers.h
3  * Copyright (C) 2016-2017 Will Schmidt <will_schmidt@vnet.ibm.com>
4  *
5  * This file contains helper functions for the ISA 3.0 test suite.
6  */
7 
8 /*
9  *   This program is free software; you can redistribute it and/or
10  *   modify it under the terms of the GNU General Public License V2
11  *   as published by the Free Software Foundation
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 #include "tests/malloc.h"       // memalign32
24 
25 typedef uint64_t  HWord_t;
26 
27 #if defined (DEBUG_ARGS_BUILD)
28 #define AB_DPRINTF(fmt, args...) do { fprintf(stderr, fmt , ##args); } while (0)
29 #else
30 #define AB_DPRINTF(fmt, args...) do { } while (0)
31 #endif
32 
33 /* Exhaustive tests?
34  * Due to the excessive size of the test results, allow a #ifdef to
35  * enable/disable most of the input values.
36  * Off by default.
37  */
38 // #define EXHAUSTIVE_TESTS 1
39 
40 
41 #define ALLCR "cr0","cr1","cr2","cr3","cr4","cr5","cr6","cr7"
42 
43 #define SET_CR(_arg) \
44       __asm__ __volatile__ ("mtcr  %0" : : "b"(_arg) : ALLCR );
45 
46 #define SET_CR0_FIELD(_arg) __asm__ __volatile__ ("mtocrf 0x80,%0 " : : "b" (_arg):"cr0");
47 #define SET_CR1_FIELD(_arg) __asm__ __volatile__ ("mtocrf 0x40,%0 " : : "b" (_arg):"cr1");
48 #define SET_CR2_FIELD(_arg) __asm__ __volatile__ ("mtocrf 0x20,%0 " : : "b" (_arg):"cr2");
49 #define SET_CR3_FIELD(_arg) __asm__ __volatile__ ("mtocrf 0x10,%0 " : : "b" (_arg):"cr3");
50 #define SET_CR4_FIELD(_arg) __asm__ __volatile__ ("mtocrf 0x08,%0 " : : "r" (_arg):"cr4");
51 #define SET_CR5_FIELD(_arg) __asm__ __volatile__ ("mtocrf 0x04,%0 " : : "r" (_arg):"cr5");
52 #define SET_CR6_FIELD(_arg) __asm__ __volatile__ ("mtocrf 0x02,%0 " : : "r" (_arg):"cr6");
53 #define SET_CR7_FIELD(_arg) __asm__ __volatile__ ("mtocrf 0x01,%0 " : : "r" (_arg):"cr7");
54 
55 #define SET_XER(_arg) \
56       __asm__ __volatile__ ("mtxer %0" : : "b"(_arg) : "xer" );
57 
58 #define GET_CR(_lval) \
59       __asm__ __volatile__ ("mfcr %0"  : "=b"(_lval) )
60 
61 #define GET_XER(_lval) \
62       __asm__ __volatile__ ("mfxer %0" : "=b"(_lval) )
63 
64 #define SET_CR_ZERO \
65       SET_CR(0)
66 
67 #define SET_FPSCR_ZERO                                        \
68    do {                                                       \
69       double _d = 0.0;                                        \
70       __asm__ __volatile__ ("mtfsf 0xFF, %0" : : "f"(_d) );   \
71    } while (0);
72 
73 #define GET_FPSCR(_arg) \
74   __asm__ __volatile__ ("mffs %0"  : "=f"(_arg) );
75 
76 /*  The bit definitions for the FPSCR are as follows.
77 Bit(s) Description
78 0:31 Reserved
79 32 Floating-Point Exception Summary (FX)
80 33 Floating-Point Enabled Exception Summary (FEX)
81 34 Floating-Point Invalid Operation Exception Summary (VX)
82 35 Floating-Point Overflow Exception (OX)
83 36 Floating-Point Underflow Exception (UX)
84 37 Floating-Point Zero Divide Exception (ZX)
85 38 Floating-Point Inexact Exception (XX)
86 39 Floating-Point Invalid Operation Exception (SNaN) (VXSNAN)
87 40 Floating-Point Invalid Operation Exception (∞ - ∞) (VXISI)
88 41 Floating-Point Invalid Operation Exception (∞ ÷ ∞) (VXIDI)
89 42 Floating-Point Invalid Operation Exception (0 ÷ 0) (VXZDZ)
90 43 Floating-Point Invalid Operation Exception (∞ × 0) (VXIMZ)
91 44 Floating-Point Invalid Operation Exception (Invalid Compare) (VXVC)
92 45 Floating-Point Fraction Rounded (FR)
93 46 Floating-Point Fraction Inexact (FI)
94 47:51 Floating-Point Result Flags (FPRF)
95 47 Floating-Point Result Class Descriptor (C)
96 48:51 Floating-Point Condition Code (FPCC)
97       48 Floating-Point Less Than or Negative (FL or <)
98       49 Floating-Point Greater Than or Positive (FG or >)
99       50 Floating-Point Equal or Zero (FE or =)
100       51 Floating-Point Unordered or NaN (FU or ?)
101 52 Reserved
102 53 Floating-Point Invalid Operation Exception (Software-Defined Condition) (VXSOFT)
103 54 Floating-Point Invalid Operation Exception (Invalid Square Root) (VXSQRT)
104 55 Floating-Point Invalid Operation Exception (Invalid Integer Convert) (VXCVI)
105 56 Floating-Point Invalid Operation Exception Enable (VE)
106 57 Floating-Point Overflow Exception Enable (OE)
107 58 Floating-Point Underflow Exception Enable (UE)
108 59 Floating-Point Zero Divide Exception Enable (ZE)
109 60 Floating-Point Inexact Exception Enable (XE)
110 61 Floating-Point Non-IEEE Mode (NI)
111 62:63 Floating-Point Rounding Control (RN)
112    00 Round to Nearest
113    01 Round toward Zero
114    10 Round toward +Infinity
115    11 Round toward -Infinity
116 */
117 /* NOTE, currently Valgrind only tracks the rounding mode, C and FPCC fields in the
118  *       FPSCR register.
119  */
120 
121 static char * fpscr_strings[] = {
122 " 0-RSVD", " 1-RSVD", " 2-RSVD", " 3-RSVD", " 4-RSVD", " 5-RSVD", " 6-RSVD",
123 " 7-RSVD", " 8-RSVD", " 9-RSVD", "10-RSVD", "11-RSVD", "12-RSVD", "13-RSVD",
124 "14-RSVD", "15-RSVD", "16-RSVD", "17-RSVD", "18-RSVD", "19-RSVD", "20-RSVD",
125 "21-RSVD", "22-RSVD", "23-RSVD", "24-RSVD", "25-RSVD", "26-RSVD", "27-RSVD",
126 "28-RSVD", "29-DRN0", "30-DRN1", "31-DRN2",
127 /* 32 */ "FX", "FEX", "VX",
128 /* 35 */ "OX", "UX", "ZX", "XX", "VXSNAN",
129 /* 40 */ "VXISI (inf-inf)", "VXIDI (inf/inf)", "VXZDZ (0/0)",
130 /* 43 */ "VXIMZ (inf*0)", "VXVC",
131 /* 45 */ "FR", "FI",
132 /* 47 */ "FPRF-C", "FPCC-FL", "FPCC-FG",
133 /* 50 */ "FPCC-FE", "FPCC-FU",
134 /* 52 */ "52-RSVD", "FXSOFT", "VXSQRT",
135 /* 55 */ "VXCVI", "VE", "OE", "UE", "ZE",
136 /* 60 */ "XE", "NI", "RN-bit62", "RN-bit63"
137 };
138 
139 #define FPCC_C_BIT    (0x1 << (63-47))
140 #define FPCC_FL_BIT   (0x1 << (63-48))
141 #define FPCC_FG_BIT   (0x1 << (63-49))
142 #define FPCC_FE_BIT   (0x1 << (63-50))
143 #define FPCC_FU_BIT   (0x1 << (63-51))
144 #define FPCC_FPRF_MASK  FPCC_C_BIT|FPCC_FL_BIT|FPCC_FG_BIT|FPCC_FE_BIT|FPCC_FU_BIT
145 
146 #define FPSCR_RN_BIT62   (0x1 << (63-62))
147 #define FPSCR_RN_BIT63   (0x1 << (63-63))
148 
149 #define CRFIELD_BIT0 0x8
150 #define CRFIELD_BIT1 0x4
151 #define CRFIELD_BIT2 0x2
152 #define CRFIELD_BIT3 0x1
153 
154 /* dissect_cr*:
155  * display the condition register bits in a
156  * human readable format.
157  */
158 
cr_overflow_set(unsigned this_cr)159 inline int cr_overflow_set(unsigned this_cr) {
160    return (this_cr & CRFIELD_BIT3);
161 }
162 
cr_zero_set(unsigned this_cr)163 inline int cr_zero_set(unsigned this_cr) {
164    return (this_cr & CRFIELD_BIT2);
165 }
166 
cr_positive_set(unsigned this_cr)167 inline int cr_positive_set(unsigned this_cr) {
168    return (this_cr & CRFIELD_BIT1);
169 }
170 
cr_negative_set(unsigned this_cr)171 inline int cr_negative_set(unsigned this_cr) {
172    return (this_cr & CRFIELD_BIT0);
173 }
174 
175 /* __dissect_cr takes a bitfield directly, not the full condition register.
176  * This is a helper for dissect_cr_rn.
177  */
__dissect_cr(unsigned this_cr)178 inline static void __dissect_cr(unsigned this_cr) {
179 if (cr_negative_set(this_cr))
180    printf("%s(LT)", verbose ? " 0x1=Negative" : "");
181 
182 if (cr_positive_set(this_cr))
183    printf("%s(GT)", verbose ? " 0x2=Positive" : "");
184 
185 if (cr_zero_set(this_cr))
186    printf("%s(EQ)", verbose ? " 0x4=Zero" : "");
187 
188 if (cr_overflow_set(this_cr))
189    printf("%s(SO)", verbose ? " 0x8=Overflow" : "");
190 }
191 
192 /* Extract one CR field */
extract_cr_rn(unsigned long local_cr,unsigned long rn)193 static int extract_cr_rn(unsigned long local_cr,unsigned long rn) {
194    unsigned int masked_cr;
195    unsigned long shifted_value;
196 
197    shifted_value = local_cr >> ( ( (7 - rn) * 4 ) );
198    masked_cr = shifted_value & 0xf;
199    return masked_cr;
200 }
201 
202 /* Display one CR field */
dissect_cr_rn(unsigned long local_cr,unsigned long rn)203 static void dissect_cr_rn(unsigned long local_cr, unsigned long rn) {
204    unsigned int masked_cr;
205 
206    masked_cr = extract_cr_rn(local_cr, rn);
207    __dissect_cr(masked_cr);
208 }
209 
210 /* Display all of the CR fields... */
dissect_cr(unsigned long local_cr)211 static void dissect_cr(unsigned long local_cr) {
212    unsigned int crn;
213 
214    for (crn = 0; crn < 8; crn++) {
215       dissect_cr_rn(local_cr, crn);
216    }
217 }
218 
219 /* dissect the fpscr bits that are valid under valgrind.
220  * Valgrind tracks the C (FPSCR[47]), FPCC (FPSCR[48:51)
221  * DRN (FPSCR[29:31]) and RN (FPSCR[62:63]).
222  */
dissect_fpscr_valgrind(unsigned long local_fpscr)223 static void dissect_fpscr_valgrind(unsigned long local_fpscr) {
224    int i;
225    long mybit;
226 
227    /* Print DRN fields */
228    for (i = 29; i < 32; i++) {
229       mybit = 1LL << (63 - i);
230       if (mybit & local_fpscr) {
231          printf(" %s",fpscr_strings[i]);
232       }
233    }
234 
235    /* Print C and FPCC fields */
236    for (i = 47; i < 52; i++) {
237       mybit = 1LL << (63 - i);
238       if (mybit & local_fpscr) {
239          printf(" %s",fpscr_strings[i]);
240       }
241    }
242 
243    /* Print RN field */
244    for (i = 62; i < 64; i++) {
245       mybit = 1LL << (63 - i);
246       if (mybit & local_fpscr) {
247          printf(" %s",fpscr_strings[i]);
248       }
249    }
250 }
251 
252 /* dissect the fpscr bits.
253  * This prints the entire FPSCR field.  This is only called under higher
254  * verbosities, as valgrind does not track most of these bits.
255  */
dissect_fpscr_raw(unsigned long local_fpscr)256 static void dissect_fpscr_raw(unsigned long local_fpscr) {
257 /* Due to the additional involved logic, the rounding mode (RN) bits 61-62
258  * are handled within dissect_fpscr_rounding_mode(). */
259    int i;
260    long mybit;
261 
262    for (i = 0; i < 61; i++) {
263       /* also note that the bit numbering is backwards. */
264       mybit = 1LL << (63 - i);
265       if (mybit & local_fpscr) {
266          printf(" %s", fpscr_strings[i]);
267       }
268    }
269 }
270 
dissect_fpscr(unsigned long local_fpscr)271 static void dissect_fpscr(unsigned long local_fpscr) {
272    if (verbose > 1) {
273       printf(" [[ fpscr:%lx ]] ", local_fpscr);
274       dissect_fpscr_raw(local_fpscr);
275    } else {
276       dissect_fpscr_valgrind(local_fpscr);
277    }
278 }
279 
280 /* Display the rounding mode */
dissect_fpscr_rounding_mode(unsigned long local_fpscr)281 static void dissect_fpscr_rounding_mode(unsigned long local_fpscr) {
282    /* special case handing for the rounding mode round-nearest (RN) bits. 62:63 */
283    printf("Rounding Mode: ");
284 
285    if (local_fpscr & FPSCR_RN_BIT62)
286       if (local_fpscr & FPSCR_RN_BIT63)
287          /* 0b11 */ printf("RN-to--INF");
288       else
289          /* 0b10 */ printf("RN-to-+INF");
290    else
291       if (local_fpscr & FPSCR_RN_BIT63)
292          /* 0b01 */ printf("RN-to-Nearest");
293       else
294          /* 0b00 */ printf("RN-to-Zero");
295 }
296 
297 /*
298  * Arithmetic, rounding, and Convert From Integer instructions will set
299  * bits in the FPCC field to indicate the class of the result.
300  * The table is described as follows;
301      flags / Result value class
302   C < > = ?
303   1 0 0 0 1 Quiet NaN
304   0 1 0 0 1 -Infinity
305   0 1 0 0 0 -Normalized Number
306   1 1 0 0 0 -Denormalized Number
307   1 0 0 1 0 -Zero
308   0 0 0 1 0 +Zero
309   1 0 1 0 0 +Denormalized Number
310   0 0 1 0 0 +Normalized Number
311   0 0 1 0 1 +Infinity
312 */
313 
dissect_fpscr_result_value_class(unsigned long local_fpscr)314 static void dissect_fpscr_result_value_class(unsigned long local_fpscr) {
315    if (local_fpscr & FPCC_C_BIT) {
316       if (local_fpscr & FPCC_FL_BIT)
317             printf("-Denormalized");
318 
319       else if (local_fpscr & FPCC_FG_BIT)
320             printf("+Denormalized");
321 
322       else if (local_fpscr & FPCC_FE_BIT)
323             printf("-Zero        ");
324 
325       else if (local_fpscr & FPCC_FU_BIT)
326             printf("Quiet NaN    ");
327 
328    } else {
329       if (local_fpscr & FPCC_FL_BIT) {
330          if (local_fpscr & FPCC_FU_BIT)
331             printf("-Infinity    ");
332 
333          else
334             printf("-Normalized  ");
335 
336       } else if (local_fpscr & FPCC_FG_BIT) {
337          if (local_fpscr & FPCC_FU_BIT)
338             printf("+Infinity    ");
339 
340          else
341             printf("+Normalized  ");
342 
343          if (local_fpscr & FPCC_FE_BIT)
344             printf("+Zero        ");
345       }
346    }
347 }
348 
349 /* Interpret the fields in the FPCC as they apply to the DCMX checks.
350  * The 'Match' indicator will typically be evaluated by the caller.
351  *
352  *  DMCX:
353     * DCMX bit / 0x value / Data Class
354     *        0       0x01  NaN
355     *        1       0x02  +Infinity
356     *        2       0x04  -Infinity
357     *        3       0x08  +Zero
358     *        4       0x10  -Zero
359     *        5       0x20  +Denormal
360     *        6       0x40  -Denormal
361     *        7       0x7f  ALL bits set.
362 */
363 
dissect_fpscr_dcmx_indicator(unsigned long local_fpscr)364 static void dissect_fpscr_dcmx_indicator(unsigned long local_fpscr) {
365    if (verbose > 2) printf("fpscr_cc:%lx ", local_fpscr & (FPCC_FPRF_MASK) );
366 
367    // See if the data class of the src value matches the set DCMX bits.
368    if (verbose > 1) printf("%s ", (local_fpscr&FPCC_FE_BIT) ? "Match":"");
369 
370    // Display the sign bit of the src value.
371    if (verbose > 1) printf("SRC sign:%s ", (local_fpscr&FPCC_FL_BIT) ? "-" : "+");
372 
373    // The src value can be either a SP or DP value, this indicates
374    // if it is a valid SP value.
375    if (verbose > 1) printf("%s ", (local_fpscr&FPCC_FE_BIT) ? "SP" : "");
376 }
377 
378 /* dissect_xer helpers*/
379 static char * xer_strings[] = {
380 " 0-RSVD", " 1-RSVD", " 2-RSVD", " 3-RSVD", " 4-RSVD", " 5-RSVD", " 6-RSVD",
381 " 7-RSVD", " 8-RSVD", " 9-RSVD", "10-RSVD", "11-RSVD", "12-RSVD", "13-RSVD",
382 "14-RSVD", "15-RSVD", "16-RSVD", "17-RSVD", "18-RSVD", "19-RSVD",
383 "20-RSVD", "21-RSVD", "22-RSVD", "23-RSVD", "24-RSVD", "25-RSVD",
384 "26-RSVD", "27-RSVD", "28-RSVD", "29-RSVD", "30-RSVD", "31-RSVD",
385 /* 32 */ "SO", "OV", "CA",
386 /* 35 */ "35-RSVD", "36-RSVD", "37-RSVD", "38-RSVD", "39-RSVD",
387 /* 40 */ "40-RSVD", "41-RSVD", "42-RSVD", "43-RSVD",
388 /* 44 */ "OV32", "CA32",
389 /* 46 */ "46-RSVD", "47-RSVD", "48-RSVD", "49-RSVD", "50-RSVD", "51-RSVD",
390          "52-RSVD", "53-RSVD", "54-RSVD", "55-RSVD", "56-RSVD",
391 /* 57:63 # bytes transferred by a Load/Store String Indexed instruction. */
392          "LSI/SSI-0", "LSI/SSI-1", "LSI/SSI-2", "LSI/SSI-3",
393          "LSI/SSI-4", "LSI/SSI-5", "LSI/SSI-6",
394 };
395 
396 /* Dissect the XER register contents.
397  */
dissect_xer_raw(unsigned long local_xer)398 static void dissect_xer_raw(unsigned long local_xer) {
399    int i;
400    long mybit;
401 
402    for (i = 0; i <= 63; i++) {
403       mybit = 1ULL << (63 - i); /* compensate for reversed bit numbering. */
404       if (mybit & local_xer)
405          printf(" %s", xer_strings[i]);
406    }
407 }
408 
409 /* */
dissect_xer(unsigned long local_xer)410 static void dissect_xer(unsigned long local_xer) {
411    if (verbose > 1)
412       printf(" [[ xer:%lx ]]", local_xer);
413    dissect_xer_raw(local_xer);
414 }
415 
416 
417 /* DFP helpers for bcd-to-dpd, dpd-to-bcd, misc.
418  * pulled from vex/.../host_generic_simd64.c
419  */
420 /*------------------------------------------------------------------*/
421 /* Decimal Floating Point (DFP) helper functions */
422 /*------------------------------------------------------------------*/
423 #define NOT( x )    ( ( ( x ) == 0) ? 1 : 0)
424 #define GET( x, y ) ( ( ( x ) & ( 0x1UL << ( y ) ) ) >> ( y ) )
425 #define PUT( x, y ) ( ( x )<< ( y ) )
426 
dpb_to_bcd(unsigned long chunk)427 static unsigned long dpb_to_bcd( unsigned long chunk )
428 {
429    int a, b, c, d, e, f, g, h, i, j, k, m;
430    int p, q, r, s, t, u, v, w, x, y;
431    unsigned long value;
432 
433    /* convert 10 bit densely packed BCD to BCD */
434    p = GET( chunk, 9 );
435    q = GET( chunk, 8 );
436    r = GET( chunk, 7 );
437    s = GET( chunk, 6 );
438    t = GET( chunk, 5 );
439    u = GET( chunk, 4 );
440    v = GET( chunk, 3 );
441    w = GET( chunk, 2 );
442    x = GET( chunk, 1 );
443    y = GET( chunk, 0 );
444 
445    /* The BCD bit values are given by the following boolean equations.*/
446    a = ( NOT(s) & v & w ) | ( t & v & w & s ) | ( v & w & NOT(x) );
447    b = ( p & s & x & NOT(t) ) | ( p & NOT(w) ) | ( p & NOT(v) );
448    c = ( q & s & x & NOT(t) ) | ( q & NOT(w) ) | ( q & NOT(v) );
449    d = r;
450    e = ( v & NOT(w) & x ) | ( s & v & w & x ) | ( NOT(t) & v & x & w );
451    f = ( p & t & v & w & x & NOT(s) ) | ( s & NOT(x) & v ) | ( s & NOT(v) );
452    g = ( q & t & w & v & x & NOT(s) ) | ( t & NOT(x) & v ) | ( t & NOT(v) );
453    h = u;
454    i = ( t & v & w & x ) | ( s & v & w & x ) | ( v & NOT(w) & NOT(x) );
455    j = ( p & NOT(s) & NOT(t) & w & v ) | ( s & v & NOT(w) & x )
456       | ( p & w & NOT(x) & v ) | ( w & NOT(v) );
457    k = ( q & NOT(s) & NOT(t) & v & w ) | ( t & v & NOT(w) & x )
458       | ( q & v & w & NOT(x) ) | ( x & NOT(v) );
459    m = y;
460 
461    value = PUT(a, 11) | PUT(b, 10) | PUT(c, 9) | PUT(d, 8) | PUT(e, 7)
462                | PUT(f, 6) | PUT(g, 5) | PUT(h, 4) | PUT(i, 3) | PUT(j, 2)
463                | PUT(k, 1) | PUT(m, 0);
464     return value;
465 }
466 #undef NOT
467 #undef GET
468 #undef PUT
469 
470 
471 typedef union dfp_union {
472    _Decimal128  dec_val128;
473    struct {
474 #if defined(VGP_ppc64le_linux)
475    unsigned long vall;
476    unsigned long valu;
477 #else
478    unsigned long valu;
479    unsigned long vall;
480 #endif
481    } u128;
482 } dfp_val_t;
483 
484 /* Based on and enhanced from the dfp128_vals table in test_dfp5.c.
485  * Todo: Refine/refactor and turn into a build_table function.
486  */
487 
488 static unsigned long dfp128_vals[] = {
489 #ifdef EXHAUSTIVE_TESTS
490    // Some finite numbers
491    0x2208000000000000ULL, 0x0000000000000001ULL, //  1 *10^0
492    0xa208800000000000ULL, 0x0000000000000001ULL, // -1 *10^1
493    0x0000000000000000ULL, 0x0000000000000001ULL, //  1 *10^-6176. (smallest exp)
494    0x43ffc00000000000ULL, 0x0000000000000001ULL, //  1 *10^6111
495    0x6fffc00000000000ULL, 0x0000000000000001ULL, // foo *10^2015.
496    0x67ffc00000000000ULL, 0x0000000000000001ULL, // foo *10^-2081.
497    0x77ffc00000000000ULL, 0x0000000000000001ULL, //  1 *10^6111 (largest exp)
498    0x77ffffffffffffffULL, 0xffffffffffffffffULL, // max possible value *10^6111 (largest exp)
499    0x0000000000000000ULL, 0x0000000000000001ULL, // min possible value 1 *10^-6176. (smallest exp)
500    0x8000000000000000ULL, 0x0000000000000001ULL, // -1 *10^-6176. (smallest exp)
501 
502    /* data bits sprinkled across the significand field. */
503    0xa208800001000000ULL, 0x0000000000010000ULL, //-foo *10^1
504    0xa208800000000100ULL, 0x0000000000000100ULL, //-foo *10^1
505    0xa208800000000000ULL, 0x0000100000000000ULL, //-foo *10^1
506    0xa208800000000000ULL, 0x0000000001000000ULL, //-foo *10^1
507    0xa208800000000000ULL, 0x0000000000000001ULL, //-foo *10^1
508 
509    // pre-existing dfp128 values:
510    0x2207c00000000000ULL, 0x0000000000000e50ULL, // foo * 10^-1
511    0x2207c00000000000ULL, 0x000000000014c000ULL, // foo * 10^-1
512    0xa207c00000000000ULL, 0x00000000000000e0ULL, // foo * 10^-1
513    0x2206c00000000000ULL, 0x00000000000000cfULL, // foo * 10^-5
514    0xa205c00000000000ULL, 0x000000010a395bcfULL, // foo * 10^-9
515    0x6209400000fd0000ULL, 0x00253f1f534acdd4ULL, // foo * 10^-4091
516    0x000400000089b000ULL, 0x0a6000d000000049ULL, // very small number // foo * 10^-6160
517 
518    // flavors of zero
519    0x2208000000000000ULL, 0x0000000000000000ULL, // 0*10^256
520    0xa208000000000000ULL, 0x0000000000000000ULL, // -0*10^0
521    0xa248000000000000ULL, 0x0000000000000000ULL, // 0*10^256
522 
523    // flavors of NAN
524    0x7c00000000000000ULL, 0x0000000000000000ULL, // quiet
525    0xfc00000000000000ULL, 0xc00100035b007700ULL, // NAN
526    0x7e00000000000000ULL, 0xfe000000d0e0a0d0ULL, // signaling NAN
527 
528    // flavors of Infinity
529    0x7800000000000000ULL, 0x0000000000000000ULL, // +inf
530    0xf800000000000000ULL, 0x0000000000000000ULL, // -inf
531    0xf900000000000000ULL, 0x0000000000000000ULL  // -inf
532 #else
533    0x2208000000000000ULL, 0x0000000000000001ULL, //  1 *10^0
534    0x77ffffffffffffffULL, 0xffffffffffffffffULL, // max possible value *10^6111 (largest exp)
535    0xa208000000000000ULL, 0x0000000000000000ULL, // -0*10^0
536    0xfc00000000000000ULL, 0xc00100035b007700ULL, // NAN
537    0x7e00000000000000ULL, 0xfe000000d0e0a0d0ULL, // signaling NAN
538    0xf800000000000000ULL, 0x0000000000000000ULL, // -inf
539 #endif
540 };
541 
542 #define NUM_DFP128_VALS (sizeof(dfp128_vals) / 8)
543 unsigned long nb_dfp128_vals = NUM_DFP128_VALS;
544 
545 /* Todo: update dfp64_vals to match dfp128_vals content. */
546 
547 static unsigned long dfp64_vals[] = {
548 #ifdef EXHAUSTIVE_TESTS
549    0x77fcffffffffffffULL, // max possible value 9..9 *10^369 (largest exp)
550    0x0000000000000001ULL, // min possible nonzero value 1 *10^-398. (smallest exp)
551    0x4248000000000001ULL, // 1*10^260
552    0x2234000000000e50ULL, // foo*10^-1
553    0x223400000014c000ULL, //
554    0xa2340000000000e0ULL, //
555    0x22240000000000cfULL, // foo*10^-5
556    0xa21400010a395bcfULL, // negative -foo*10^-9
557    0x6e4d3f1f534acdd4ULL, // huge number foo*10^5
558    0x000400000089b000ULL, // very small number foo*10^-397
559 
560    // flavors of zero
561    0x2238000000000000ULL,
562    0xa238000000000000ULL, // 0 * 10 ^0
563    0x4248000000000000ULL, // 0 * 10 ^260
564 
565    // flavors of NAN
566    0x7e34000000000111ULL, //signaling NaN
567    0xfe000000d0e0a0d0ULL, //signaling NaN
568    0xfc00000000000000ULL, //quiet NaN
569 
570    // flavors of Infinity
571    0x7800000000000000ULL, //+Inf
572    0xf800000000000000ULL, //-Inf
573    0x7a34000000000000ULL, //+Inf
574 #else
575    0x77fcffffffffffffULL, // max possible value 9..9 *10^369 (largest exp)
576    0x4248000000000000ULL, // 0 * 10 ^260
577    0xfe000000d0e0a0d0ULL, //signaling NaN
578    0xf800000000000000ULL, //-Inf
579 #endif
580 };
581 
582 #define NUM_DFP64_VALS (sizeof(dfp64_vals) / 8)
583 unsigned long nb_dfp64_vals = NUM_DFP64_VALS;
584 
585 /* shift helpers */
586 #define SH_0  0
587 #define SH_1  1
588 #define SH_2  15
589 #define SH_3  63
590 
591 static uint64_t shift_amounts[] = {
592    SH_0,
593    SH_1,
594    SH_2,
595    SH_3,
596 #define SHIFT_ARRAY_SIZE 4
597 };
598 
599 /* vector splat helpers */
600 #define SPLAT0 0
601 #define SPLAT1 1
602 #define SPLAT2 0xaa
603 #define SPLAT3 0x55
604 #define SPLAT4 0xff
605 
606 static uint64_t splat_values[] = {
607    SPLAT0,
608    SPLAT1,
609    SPLAT2,
610    SPLAT3,
611    SPLAT4,
612 #define SPLAT_ARRAY_SIZE 5
613 };
614 
615 /* a small memory range used to test load-from and store-to vsx */
616 #define BUFFER_SIZE 4
617 #define MAX_BUFFER_PATTERNS 6
618 unsigned long buffer[BUFFER_SIZE];
619 
initialize_buffer(int t)620 static void initialize_buffer(int t)
621 {
622    int x;
623 
624    for (x = 0; x < BUFFER_SIZE; x++)
625       /* Don't want each of the 32-bit chunks to be identical. Loads of a
626        * byte from the wrong 32-bit chuck are not detectable if the chunks
627        * are identical.
628        */
629       switch((t+x)%BUFFER_SIZE) {
630       case 0:
631          buffer[x] = 0xffffffffffffffff;
632          break;
633       case 1:
634          buffer[x] = 0x0001020304050607;
635          break;
636       case 2:
637          buffer[x] = 0x5555555555555555;
638          break;
639       case 3:
640          buffer[x] = 0x0000000000000000;
641          break;
642       case 4:
643          buffer[x] = 0x5a05a05a05a05a05;
644          break;
645       case 5:
646          buffer[x] = 0x0102030405060708;
647          break;
648       default:
649          buffer[x] = 0x1010101010101010;
650          break;
651    }
652 }
653 
654 #define PATTERN_SIZE 5
655 unsigned long pattern[PATTERN_SIZE] = {
656 	0xffffffffffffffff,
657 	0xaaaaaaaaaaaaaaaa,
658 	0x5152535455565758,
659 	0x0000000000000000,
660 	0xffaa5599113377cc,
661 };
662 
663 
dump_small_buffer(void)664 static void dump_small_buffer(void) {
665    int x;
666 
667    printf("[ ");
668 
669    for (x = 0; x < BUFFER_SIZE; x++)
670       printf("%016lx ", buffer[x] );
671 
672    printf("]");
673 }
674 
675 /* value to be shifted */
676 static uint64_t values_to_shift[] = {
677                   0x0,
678                   0x1,
679                  0x10,
680                 0x100,
681                0x1000,
682               0x10000,
683              0x100000,
684             0x1000000,
685            0x10000000,
686           0x100000000,
687          0x1000000000,
688         0x10000000000,
689        0x100000000000,
690       0x1000000000000,
691      0x10000000000000,
692     0x100000000000000,
693    0x1000000000000000,
694                   0xf,
695                  0x1f,
696                 0x10f,
697                0x100f,
698               0x1000f,
699              0x10000f,
700             0x100000f,
701            0x1000000f,
702           0x10000000f,
703          0x100000000f,
704         0x1000000000f,
705        0x10000000000f,
706       0x100000000000f,
707      0x1000000000000f,
708     0x10000000000000f,
709    0x100000000000000f,
710                   0x7,
711                  0x70,
712                 0x700,
713                0x7000,
714               0x70000,
715              0x700000,
716             0x7000000,
717            0x70000000,
718           0x700000000,
719          0x7000000000,
720         0x70000000000,
721        0x700000000000,
722       0x7000000000000,
723      0x70000000000000,
724     0x700000000000000,
725    0x7000000000000000,
726                   0x8,
727                  0x80,
728                 0x800,
729                0x8000,
730               0x80000,
731              0x800000,
732             0x8000000,
733            0x80000000,
734           0x800000000,
735          0x8000000000,
736         0x80000000000,
737        0x800000000000,
738       0x8000000000000,
739      0x80000000000000,
740     0x800000000000000,
741    0x8000000000000000,
742    0xffffffffffffffff,
743    0
744 #define SHIFT_VALUES_SIZE 66
745 };
746 
747 /* DFP related helper functions: */
748 
749 /* For DFP finite numbers, the combination field (G field) is a
750  * combination of the exponent and the LMD (Left Most Digit) of the
751  * significand.  The fields are encoded/decoded as described in the
752  * table here.
753  *       00       01      10   -< Exponent bits.
754  * 0:   00000   01000   10000
755  * ...
756  * 7:   00111   01111   10111
757  * 8:   11000   11010   11100
758  * 9:   11001   11011   11101  (encoded special field).
759  * |
760  * ^ LMD value.
761 */
762 #define DFP_GFIELD_MASK  0x7c00000000000000UL
763 #define DFP_GFIELD_SHIFT 58
764 
special_field_LMD(uint64_t dword1)765 static unsigned int special_field_LMD(uint64_t dword1) {
766    unsigned long g_field_specials;
767    int left_two_bits;
768    int right_three_bits;
769 
770    g_field_specials = (dword1 & DFP_GFIELD_MASK) >> DFP_GFIELD_SHIFT;
771    left_two_bits = (g_field_specials & 0x18) >> 3;
772    right_three_bits = g_field_specials & 0x07;
773 
774    /* The LMD result maps directly to the right_three_bits value as
775     * long as the left two bits are 0b00,0b01,0b10.  So a compare
776     * against 3 is sufficient to determine if we can return the right
777     * three bits directly.  (LMD values 0..7).
778     */
779    if (left_two_bits < 3) {
780       return (right_three_bits);
781    }
782 
783    /* LMD values of 8 or 9 require a bit of swizzle, but a check of
784     * the right-most bit is sufficient to determine whether LMD value
785     * is 8 or 9.
786     */
787    if (right_three_bits & 0x1)
788       return 9;
789    else
790       return 8;
791 }
792 
793 /* Returns the exponent bits, as decoded from the G field. */
special_field_exponent_bits(unsigned long dword1)794 static inline int special_field_exponent_bits(unsigned long dword1) {
795    unsigned long g_field_specials;
796    int left_two_bits;
797    int right_three_bits;
798 
799    g_field_specials = (dword1 & DFP_GFIELD_MASK) >> DFP_GFIELD_SHIFT;
800    left_two_bits = (g_field_specials & 0x18) >> 3;
801    right_three_bits = g_field_specials & 0x07;
802 
803    /* The special field exponent bits maps directly to the left_two_bits
804     * value as long as the left two bits are 0b00,0b01,0b10.  So a compare
805     * against 3 is sufficient for those values.
806     */
807    if (left_two_bits < 3) {
808       return (left_two_bits);
809    }
810 
811    switch(right_three_bits) {
812       case 0:
813       case 1: return 0x0;
814       case 2:
815       case 3: return 0x1;
816       case 4:
817       case 5: return 0x2;
818       case 6: /* Infinity */ return 0x0;
819       case 7: /* NaN */  return 0x0;
820    }
821    return -1;  /* should never hit this */
822 }
823 
824 /* get_declet().  Return a 10-bit declet, beginning at the 'start'
825  * offset.
826  *
827  * | dword1 | dword0 |
828  * | 0    63|64   127|
829  */
830 #define TEN_BITS 0x03ffULL
831 
get_declet(int start,uint64_t dword1,uint64_t dword0)832 static inline int get_declet(int start, uint64_t dword1, uint64_t dword0) {
833    unsigned long local_declet;
834    unsigned int dword0_shift;
835    unsigned int dword1_shift;
836 
837    dword1_shift = 63 - (start + 9);
838    dword0_shift = 127 - (start + 9);
839 
840    if (verbose>5) printf("\n%s (%d) %016lx %016lx",
841                          __FUNCTION__, start, dword1, dword0);
842 
843    if ((start + 9) < 63) { /* fully within dword1 */
844       local_declet = (dword1 >> dword1_shift) & TEN_BITS;
845 
846    } else if (start >= 65) {/* fully within dword0 */
847       local_declet = (dword0 >> dword0_shift) & TEN_BITS;
848 
849    } else { /* straddling the two dwords*/
850       unsigned long mask_dword0;
851       unsigned long mask_dword1;
852 
853       mask_dword1 = TEN_BITS >> (64 - dword0_shift);
854       mask_dword0 = TEN_BITS << (dword0_shift);
855       local_declet =
856          ((dword1 & mask_dword1) << (64-dword0_shift)) +
857          ((dword0 & mask_dword0) >> dword0_shift);
858    }
859    return local_declet;
860 }
861 
get_bcd_digit_from_dpd(int start,uint64_t dword1,uint64_t dword0)862 static int get_bcd_digit_from_dpd(int start, uint64_t dword1,
863                                   uint64_t dword0) {
864    long bcd_digit;
865    long declet;
866 
867    declet = get_declet(start, dword1, dword0);
868    bcd_digit = dpb_to_bcd(declet);
869    return bcd_digit;
870 }
871 
872 
873 /* The 'exponent left' shift is for moving the leftmost two bits
874  * of the exponent down to where they can be easily merged with the
875  * rest of the exponent.
876  */
877 #define DFP128_EXPONENT_RIGHT_MASK       0x03ffc00000000000
878 #define DFP64_EXPONENT_RIGHT_MASK        0x03fc000000000000
879 #define DFP128_EXPONENT_RIGHT_MASK_SHIFT 46
880 #define DFP64_EXPONENT_RIGHT_MASK_SHIFT  50
881 #define DFP128_EXPONENT_LEFT_SHIFT       12
882 #define DFP64_EXPONENT_LEFT_SHIFT         8
883 
884 #define DFP_NAN                          0x1f
885 #define DFP_INF                          0x1e
886 #define DFP_SIGNALING_NAN_BIT            0x0200000000000000
887 
888 /* Start of the Trailing Significand field is at bit # .. */
889 #define DFP128_T_START         18
890 #define DFP64_T_START          14
891 
892 //The exponent bias value is 101 for DFP Short, 398
893 //for DFP Long, and 6176 for DFP Extended.
894 #define DFP128_EXPONENT_BIAS 6176
895 #define DFP64_EXPONENT_BIAS   398
896 
897 /* return the dfp exponent from the leading dword. */
dfp128_exponent(unsigned long dword1)898 static inline signed long dfp128_exponent(unsigned long dword1) {
899    unsigned long exponent_left;
900    unsigned long exponent_right;
901    unsigned long biased_exponent;
902    signed long exponent;
903 
904    exponent_left = special_field_exponent_bits(dword1);
905    exponent_right = (dword1 & DFP128_EXPONENT_RIGHT_MASK);
906    biased_exponent = (exponent_left << DFP128_EXPONENT_LEFT_SHIFT) +
907                      (exponent_right >> DFP128_EXPONENT_RIGHT_MASK_SHIFT);
908 
909    /* Unbias the exponent. */
910    exponent = biased_exponent - DFP128_EXPONENT_BIAS;
911 
912    return exponent;
913 }
914 
915 /* Interpret the paired 64-bit values as a extended (quad) 128 bit DFP.
916  *
917  * | Significand | Combination Field/    |                          |
918  * | sign bit    | Encoded Exponent      | remainder of significand |
919  * |0            |1                    17|18                     127|
920  *  ^ (bit0) Significand sign bit.
921  *                ^ (bit 1:17) Combination field. Contains high bits of
922  *                  exponent (encoded), LMD of significand (encoded),
923  *                  and the remainder of the exponent.  First five bits
924  *                  will indicate special cases NAN or INF.
925  *                                   ^ (bit 18:127) Remainder of the
926  *                                     significand.
927  */
928 
929 #define DFP128_COMBINATION_MASK      0x7fffc
930 #define DFP64_COMBINATION_MASK       0x7ffc
931 #define DFP128_COMBINATION_SHIFT     46
932 #define DFP64_COMBINATION_SHIFT      50
933 #define DFP_SPECIAL_SYMBOLS_MASK     0x1f
934 #define DFP_SPECIAL_SYMBOLS_SHIFT    58
935 
dissect_dfp128_float(uint64_t dword1,uint64_t dword0)936 static inline void dissect_dfp128_float(uint64_t dword1, uint64_t dword0) {
937    long signbit;
938    signed long exponent;
939    unsigned long gfield_special_symbols;
940    unsigned long lmd_digit;
941    unsigned long bcd_digits[13];
942    int i;
943    int silent=0; // suppress leading zeros from the output.
944 
945    if (verbose > 5) printf("RAW128: %016lx %016lx ", dword1, dword0);
946 
947    signbit = (dword1 >> 63);
948 
949    if (signbit) printf("-");
950    else         printf("+");
951 
952    gfield_special_symbols =
953       ((dword1 >> DFP_SPECIAL_SYMBOLS_SHIFT) & DFP_SPECIAL_SYMBOLS_MASK);
954 
955    switch (gfield_special_symbols) {
956       case DFP_INF:
957          printf(   "inf      ");
958          break;
959 
960       case DFP_NAN:
961          if (dword1 & DFP_SIGNALING_NAN_BIT)
962             printf("SNaN     ");
963          else
964             printf("QNaN     ");
965          break;
966 
967       default:
968          printf(   "Finite   ");
969          exponent  = dfp128_exponent(dword1);
970          lmd_digit = special_field_LMD(dword1);
971 
972          for (i = 0; i < 11; i++) {
973             bcd_digits[i] = get_bcd_digit_from_dpd((DFP128_T_START
974                                                     + 10 * i), dword1, dword0);
975          }
976 
977          if (lmd_digit) {
978             silent++;
979             printf("%01lx", lmd_digit);
980 
981          } else {
982             printf(" ");
983          }
984 
985          for (i = 0; i < 11; i++) {
986             if (bcd_digits[i] || silent ) {
987                silent++;
988                printf("%03lx", bcd_digits[i]);
989 
990             } else {
991                /* always print at least the last zero */
992                if (i == 10)
993                   printf("  0");
994 
995                else
996                   printf("   ");
997             }
998          }
999          printf(" * 10 ^ ");
1000          printf("%ld ", exponent);
1001    }
1002 }
1003 
1004 /* Interpret the 64-bit values as a 64 bit DFP.
1005 *
1006 * | Significand | Combination Field/    |                          |
1007 * | sign bit    | Encoded Exponent      | remainder of significand |
1008 * |0            |1                    13|14                      63|
1009 *  ^ (bit0) Significand sign bit.
1010 *                ^ (bit 1:13) Combination field. Contains high bits of
1011 *                  exponent (encoded), LMD of significand (encoded),
1012 *                  and the remainder of the exponent.  First five bits
1013 *                  will indicate special cases NAN or INF.
1014 *                                        ^ (bit 14:63) Remainder of the
1015 *                                          significand.
1016 */
1017 
1018 /* return the dfp exponent from the leading dword. */
dfp64_exponent(unsigned long dword1)1019 static inline signed long dfp64_exponent(unsigned long dword1) {
1020    unsigned long exponent_left;
1021    unsigned long exponent_right;
1022    unsigned long biased_exponent;
1023    signed long exponent;
1024 
1025    exponent_left = special_field_exponent_bits(dword1);
1026    exponent_right = (dword1 & DFP64_EXPONENT_RIGHT_MASK);
1027    biased_exponent = (exponent_left << DFP64_EXPONENT_LEFT_SHIFT) +
1028                      (exponent_right >> DFP64_EXPONENT_RIGHT_MASK_SHIFT);
1029 
1030    /* Unbias the exponent. */
1031    exponent = biased_exponent - DFP64_EXPONENT_BIAS;
1032    return exponent;
1033 }
1034 
dissect_dfp64_float(uint64_t dword1)1035 static inline void dissect_dfp64_float(uint64_t dword1) {
1036    long signbit;
1037    signed long exponent;
1038    unsigned long gfield_special_symbols;
1039    unsigned long lmd_digit;
1040    unsigned long bcd_digits[13];
1041    int i;
1042    int silent=0; // suppress leading zeros from the output.
1043 
1044    if (verbose > 5) printf("RAW64: %016lx ", dword1);
1045 
1046    signbit = (dword1 >> 63);
1047 
1048    if (signbit) printf("-");
1049    else         printf("+");
1050 
1051    gfield_special_symbols =
1052       ((dword1 >> DFP_SPECIAL_SYMBOLS_SHIFT) & DFP_SPECIAL_SYMBOLS_MASK);
1053 
1054    switch (gfield_special_symbols) {
1055       case DFP_INF:
1056          printf(   "inf      ");
1057          break;
1058 
1059       case DFP_NAN:
1060          if (dword1 & DFP_SIGNALING_NAN_BIT)
1061             printf("SNaN     ");
1062          else
1063             printf("QNaN     ");
1064          break;
1065 
1066       default:
1067          printf(   "Finite   ");
1068          exponent  = dfp64_exponent(dword1);
1069          lmd_digit = special_field_LMD(dword1);
1070 
1071          for (i = 0; i < 5; i++)
1072             bcd_digits[i] = get_bcd_digit_from_dpd((DFP64_T_START + 10 * i),
1073                                                    dword1, 0);
1074 
1075          if (lmd_digit) {
1076             silent++;
1077             printf("%01lx", lmd_digit);
1078 
1079          } else {
1080             printf(" ");
1081          }
1082 
1083          for (i = 0; i < 5; i++) {
1084             if (bcd_digits[i] || silent) {
1085                silent++;
1086                printf("%03lx", bcd_digits[i]);
1087 
1088             } else { // suppress leading zeros.
1089                /* always print at least the last zero */
1090                if (i == 4)
1091                   printf("  0");
1092 
1093                else
1094                   printf("   ");
1095             }
1096          }
1097          printf(" * 10 ^ ");
1098          printf("%ld ", exponent);
1099    }
1100 }
1101 
dump_dfp128_table(void)1102 static void dump_dfp128_table(void) {
1103    int i;
1104 
1105    printf("DFP 128 table:\n");
1106 
1107    for (i = 0; i < nb_dfp128_vals; i += 2) {
1108       printf("i=:%2d ", i);
1109       dissect_dfp128_float(dfp128_vals[i], dfp128_vals[i+1]);
1110       printf("\n");
1111    }
1112 }
1113 
dump_dfp64_table(void)1114 static void dump_dfp64_table(void) {
1115    int i;
1116 
1117    printf("DFP 64 table:\n");
1118 
1119    for (i = 0; i<nb_dfp64_vals; i++) {
1120       printf("i=:%2d ", i);
1121       dissect_dfp64_float(dfp64_vals[i]);
1122       printf("\n");
1123    }
1124 }
1125 
1126 
1127 /* Data Formats for floating point.
1128  * Floating point values include the following:
1129  *  -INF -NOR -DEN -0 +0 +DEN +NOR +INF
1130  *  INFinite: When the biased exponent is the MAX possible value, and
1131  *   the fraction field is 0.
1132  *  ZERo.    biased exponent is zero, fraction is 0.
1133  *  DENormalized.   biased exponent is 0, and fraction is non-zero.
1134  *  NORmalized. All other values that are neither Zero, Denormalized,
1135  *   or Infinite.  Biased exponent=1..MAX-1.
1136  */
1137 
1138 /* Quad (128bit):
1139  * | Sign | EXPonent+Bias  | FRACTION/Mantissa |
1140  *  0      1             15 16              127
1141  *  exponent is 15 bits. ranging from:  0x0000 .. 0x7fff
1142  *     0 = (zero if fraction==0, DeNormal if fraction !=0 )
1143  *     1...0x7ffe = normalized
1144  *     7fff  =  (infinite if fraction==0, NaN if fraction !=0)
1145  */
1146 #define QUAD_EXP_MASK 0x7fff
1147 
1148 /* This assumes we are working on the top half of a quad stored in a 64-bit
1149  *  register.
1150  */
1151 #define QUAD_EXP_SHIFT 48
1152 #define QUAD_MANTISSA_MASK 0x0000ffffffffffff
build_binary128_float(unsigned long signbit,unsigned long exponent,unsigned long mantissa)1153 static inline unsigned long build_binary128_float(unsigned long signbit,
1154                                                   unsigned long exponent,
1155                                                   unsigned long mantissa) {
1156    unsigned long thevalue;
1157 
1158    thevalue = (unsigned long) (signbit << 63) |
1159       ((exponent & QUAD_EXP_MASK) << QUAD_EXP_SHIFT) |
1160       (mantissa & QUAD_MANTISSA_MASK);
1161 
1162    if (verbose > 3)
1163       printf("%s %lx \n", __FUNCTION__, (unsigned long)thevalue);
1164 
1165    return thevalue;
1166 }
1167 
1168  /* double (64bit):
1169  * | Sign | EXPonent+Bias  | FRACTION/Mantissa |
1170  *  0      1             11 12              63
1171  * exponent is 11 bits. ranging from:  0x000 .. 0x7ff
1172  *    0 = (zero if fraction==0, DeNormal if fraction !=0 )
1173  *    1...0x7fe = normalized
1174  *    7ff  =  (infinite if fraction==0, NaN if fraction !=0)
1175 */
1176 #define DOUBLE_EXP_MASK 0x7ff
1177 #define DOUBLE_EXP_SHIFT 52
1178 #define DOUBLE_MANTISSA_MASK 0x000fffffffffffff
1179 
build_binary64_float(unsigned long signbit,unsigned long exponent,unsigned long mantissa)1180 static inline unsigned long build_binary64_float(unsigned long signbit,
1181                                                  unsigned long exponent,
1182                                                  unsigned long mantissa) {
1183    unsigned long  thevalue;
1184 
1185    thevalue = (unsigned long ) (signbit << 63) |
1186       ((exponent & DOUBLE_EXP_MASK) << DOUBLE_EXP_SHIFT) |
1187       (mantissa & DOUBLE_MANTISSA_MASK );
1188 
1189    if (verbose > 3)
1190       printf("%s %lx \n", __FUNCTION__, (unsigned long)thevalue);
1191 
1192    return thevalue;
1193 }
1194 
1195  /* floating point single (32bit):
1196  * | Sign | EXPonent+Bias  | FRACTION/Mantissa |
1197  *  0      1              8 9                31
1198  * exponent is 8 bits. ranging from:  0x00 .. 0xff
1199  *    0 = (zero if fraction==0, DeNormal if fraction !=0 )
1200  *    1...0x7e = normalized
1201  *    7f = (infinite if fraction==0, NaN if fraction !=0) */
1202 #define SINGLE_EXP_MASK 0xff
1203 #define SINGLE_EXP_SHIFT 23
1204 #define SINGLE_MANTISSA_MASK 0x007fffff
1205 
1206 /* This is building the 32-bit float. */
build_binary32_float(unsigned long signbit,unsigned long exponent,unsigned long mantissa)1207 static inline unsigned long build_binary32_float(unsigned long signbit,
1208                                                  unsigned long exponent,
1209                                                  unsigned long mantissa) {
1210    unsigned long thevalue;
1211    unsigned long local_signbit;
1212    unsigned long local_exponent;
1213    unsigned long local_mantissa;
1214 
1215    local_signbit  = (signbit != 0) << 31;
1216    local_exponent = ((exponent & SINGLE_EXP_MASK) << SINGLE_EXP_SHIFT);
1217    local_mantissa = (mantissa & SINGLE_MANTISSA_MASK);
1218 
1219    thevalue = (unsigned long) (local_signbit) |
1220       (local_exponent) |
1221       (local_mantissa);
1222 
1223    if (verbose > 3)
1224       printf("%s %lx \n", __FUNCTION__, (unsigned long)thevalue);
1225 
1226    return thevalue;
1227 }
1228 
1229 /* floating point half (16bit):
1230  * | Sign | EXPonent+Bias  | FRACTION/Mantissa |
1231  *  0      1              6 7               15
1232  * exponent is 6 bits.  0x00 .. 0x7e masked with EXP_MASK
1233  *    0 = (zero if fraction==0, DeNormal if fraction !=0 )
1234  *    1...0x7d = normalized
1235  *    7e = (infinite if fraction==0, NaN if fraction !=0) */
1236 /* when extracting the exponent from the 16-bit half-word, use this mask. */
1237 #define HALF_EXP_MASK 0x7e00
1238 
1239 /* when building the 16-bit half-word, mask against this,
1240  * then shift into place
1241  */
1242 #define HALF_EXP_MASK_NORMALIZED 0x3f
1243 #define HALF_EXP_SHIFT 9
1244 #define HALF_MANTISSA_MASK 0x01ff
1245 
1246 /* This is building the 16-bit float. */
build_binary16_float(unsigned long in_signbit,unsigned long exponent,unsigned mantissa)1247 static inline unsigned long build_binary16_float(unsigned long in_signbit,
1248                                                  unsigned long exponent,
1249                                                  unsigned mantissa) {
1250    unsigned long thevalue;
1251    unsigned long local_signbit;
1252    unsigned long local_exponent;
1253    unsigned long local_mantissa;
1254 
1255    local_signbit = (in_signbit != 0) << 15;
1256 
1257    local_exponent= ((exponent & HALF_EXP_MASK_NORMALIZED) << HALF_EXP_SHIFT);
1258    local_mantissa = (mantissa & HALF_MANTISSA_MASK);
1259 
1260    thevalue = (unsigned long) (local_signbit) | (local_exponent)
1261       | (local_mantissa);
1262 
1263    if (verbose > 3)
1264       printf("%s %lx \n", __FUNCTION__, (unsigned long)thevalue);
1265 
1266    return thevalue;
1267 }
1268 
1269 /* dissect_binary128_float:
1270  * Interpret the (high half) 64-bit value as normal/denormal/inf/NaN.
1271  * This is as it would be interpreted as the MSB portion of
1272  *  a 128-bit wide QUAD.
1273  */
dissect_binary128_float(uint64_t value)1274 static inline void dissect_binary128_float(uint64_t value) {
1275    unsigned long signbit;
1276    unsigned long exponent;
1277    unsigned long  mantissa;
1278 
1279    signbit  = (value >> 63);
1280    exponent = ( QUAD_EXP_MASK & (value >> QUAD_EXP_SHIFT));
1281    mantissa = ( QUAD_MANTISSA_MASK & value);
1282 
1283    if (verbose > 4) printf("128 bit:");
1284 
1285    if (signbit) printf("-");
1286    else         printf("+");
1287 
1288    switch (exponent) {
1289       case 0x0:
1290          if (mantissa == 0) printf("zero     ");
1291          else               printf("denormal ");
1292          break;
1293 
1294       case QUAD_EXP_MASK:
1295          if (mantissa == 0) printf("inf      ");
1296          else               printf("NaN      ");
1297          break;
1298 
1299       default:              printf("Normal   ");
1300    }
1301 
1302    if (verbose > 4)
1303       printf("%lx %4lx %16lx %16lx \n", signbit, exponent, mantissa, value);
1304 }
1305 
1306 /* Interpret the 64-bit value as normal/denormal/inf/NaN
1307  * this is as interpreted as the 64-bit float
1308  */
dissect_binary64_float(uint64_t value)1309 static inline void dissect_binary64_float(uint64_t value) {
1310    unsigned long signbit;
1311    unsigned long exponent;
1312    unsigned long mantissa;
1313 
1314    signbit  = (value >> 63); // bit0
1315    exponent = ( DOUBLE_EXP_MASK & (value >> DOUBLE_EXP_SHIFT));
1316    mantissa = ( DOUBLE_MANTISSA_MASK & value);
1317 
1318    if (verbose > 4) printf(" 64 bit:");
1319 
1320    if (signbit) printf("-");
1321    else         printf("+");
1322 
1323    switch (exponent) {
1324       case 0x0:
1325          if (mantissa == 0) printf("zero     ");
1326          else               printf("denormal ");
1327          break;
1328 
1329       case DOUBLE_EXP_MASK:
1330          if (mantissa == 0) printf("inf      ");
1331          else               printf("NaN      ");
1332          break;
1333 
1334       default:              printf("Normal   ");
1335    }
1336 
1337    if (verbose>4)
1338       printf("%lx %4lx %16lx %16lx\n", signbit, exponent, mantissa, value);
1339 }
1340 
1341 /* interpret the 32-bit value as normal/denormal/inf/NaN.
1342  * Note that the value is stored in the upper half of a
1343  * 64-bit, which is itself in the upper half of a quad.
1344  */
dissect_binary32_float(uint64_t value)1345 static inline void dissect_binary32_float(uint64_t value) {
1346    unsigned long signbit;
1347    unsigned long exponent;
1348    unsigned long mantissa;
1349    unsigned long adj_value;
1350 
1351    /* shift down to where the offsets make more sense.*/
1352    adj_value = value;   //>>32;
1353    signbit  = (adj_value >> 31);
1354    exponent = ( SINGLE_EXP_MASK & (adj_value >> SINGLE_EXP_SHIFT));
1355    mantissa = ( SINGLE_MANTISSA_MASK & adj_value);
1356 
1357    if (verbose > 4) printf(" 32 bit:");
1358 
1359    if (signbit) printf("-");
1360    else         printf("+");
1361 
1362    switch (exponent) {
1363       case 0x0:
1364          if (mantissa == 0) printf("zero     ");
1365          else               printf("denormal ");
1366          break;
1367 
1368       case SINGLE_EXP_MASK:
1369          if (mantissa == 0) printf("inf      ");
1370          else               printf("NaN      ");
1371          break;
1372 
1373       default:              printf("Normal   ");
1374    }
1375 
1376    if (verbose>4)
1377       printf("%lx %4lx %16lx %16lx \n", signbit, exponent, mantissa, adj_value);
1378 }
1379 
1380 /* Interpret the 16-bit value as normal/denormal/inf/NaN. */
dissect_binary16_float(uint64_t value)1381 static inline void dissect_binary16_float(uint64_t value) {
1382    unsigned long signbit;
1383    unsigned long exponent;
1384    unsigned long mantissa;
1385    unsigned long adj_value;
1386 
1387    adj_value = (value & 0xffff);
1388    signbit  = ((adj_value & 0x8000) > 1);
1389    exponent = ((adj_value & HALF_EXP_MASK ) >> HALF_EXP_SHIFT) ;
1390    mantissa = (adj_value & HALF_MANTISSA_MASK);
1391 
1392    if (verbose > 4) printf(" 16 bit:");
1393 
1394    if (signbit) printf("-");
1395    else         printf("+");
1396 
1397    switch (exponent) {
1398       case 0x0:
1399          if (mantissa == 0) printf("zero     ");
1400          else               printf("denormal ");
1401          break;
1402 
1403       case HALF_EXP_MASK:
1404          if (mantissa == 0) printf("inf      ");
1405          else               printf("NaN      ");
1406          break;
1407 
1408       default:              printf("Normal   ");
1409    }
1410 
1411    if (verbose > 4)
1412       printf("%lx %4lx %16lx %16lx \n",
1413              signbit, exponent>>HALF_EXP_SHIFT, mantissa, adj_value);
1414 }
1415 
1416 #define dissect_double_as_32s(vec_foo) \
1417   printf(" "); \
1418   dissect_binary16_float((vec_foo & 0xffffffff)); \
1419   printf(" "); \
1420   dissect_binary16_float((vec_foo >> 32) & 0xffffffff);
1421 
1422 #define dissect_double_as_16s(vec_foo) \
1423   printf(" "); \
1424   dissect_binary16_float((vec_foo&0xffff)); \
1425   printf(" "); \
1426   dissect_binary16_float((vec_foo>>16)&0xffff); \
1427   printf(" "); \
1428   dissect_binary16_float((vec_foo>>32)&0xffff); \
1429   printf(" "); \
1430   dissect_binary16_float((vec_foo>>48)&0xffff);
1431 
1432 /* a table of exponent values for use in the float precision tests. */
1433 unsigned long exponent_table[] = {
1434 #ifdef EXHAUSTIVE_TESTS
1435   0x0000,   /* +/-0 or +/-DENormalized, depending on associated mantissa. */
1436   0x1a,     /* within NORmalized for 16,32,64,128-bit.                    */
1437   0x1f,     /* +/-INF or +/-NaN for 16bit, NORmalized for 32,64,128       */
1438   0xff,     /* +/-INF or +/-NaN for 32bit, NORmalized for 64,128          */
1439   0x7ff,    /* +/-INF or +/-NaN for 32 and 64bit, NORmalized for 128      */
1440   0x7fff,   /* +/-INF or +/-NaN for 128bit.                               */
1441 #else
1442   0x0000,   /* +/-0 or +/-DENormalized, depending on associated mantissa. */
1443   0xff,     /* +/-INF or +/-NaN for 32bit, NORmalized for 64,128          */
1444   0x7ff,    /* +/-INF or +/-NaN for 32 and 64bit, NORmalized for 128      */
1445   0x7fff,   /* +/-INF or +/-NaN for 128bit.                               */
1446 #endif
1447 };
1448 #define MAX_EXPONENTS  (sizeof(exponent_table) / sizeof(unsigned long))
1449 
1450 unsigned long mantissa_table[] = {
1451 #ifdef EXHAUSTIVE_TESTS
1452   0xbeefbeefbeef, /* NOR or DEN or NaN */
1453   0x000000000000, /* ZERO or INF */
1454   0x7fffffffffff, /* NOR or DEN or NaN */
1455 #else
1456   0x000000000000, /* ZERO or INF */
1457   0x7fffffffffff, /* NOR or DEN or NaN */
1458 #endif
1459 };
1460 #define MAX_MANTISSAS (sizeof(mantissa_table) / sizeof(unsigned long))
1461 
1462 /* build in 64-bit chunks, low doubleword is zero. */
1463 static unsigned long * float_vsxargs;
1464 static unsigned long * binary128_float_vsxargs = NULL;
1465 static unsigned long * binary64_float_vsxargs = NULL;
1466 static unsigned long * binary32_float_vsxargs = NULL;
1467 static unsigned long * binary16_float_vsxargs = NULL;
1468 
1469 unsigned long nb_float_vsxargs;
1470 
1471 #define MAX_FLOAT_VSX_ARRAY_SIZE (((MAX_EXPONENTS * MAX_MANTISSAS) * 2 + 1) * 2)
1472 
build_float_vsx_tables(void)1473 void build_float_vsx_tables (void)
1474 {
1475    long i = 0;
1476    unsigned long signbit;
1477    unsigned long exponent;
1478    unsigned long mantissa;/* also referred to as FRACTION in the ISA.*/
1479    unsigned long exponent_index;
1480    unsigned long mantissa_index;
1481 
1482    if (verbose > 2) printf("%s\n", __FUNCTION__);
1483 
1484    binary128_float_vsxargs = malloc(MAX_FLOAT_VSX_ARRAY_SIZE
1485                                     * sizeof(unsigned long));
1486 
1487    float_vsxargs = binary128_float_vsxargs;
1488 
1489    binary64_float_vsxargs = malloc(MAX_FLOAT_VSX_ARRAY_SIZE
1490                                    * sizeof(unsigned long));
1491 
1492    binary32_float_vsxargs = malloc(MAX_FLOAT_VSX_ARRAY_SIZE
1493                                    * sizeof(unsigned long));
1494    binary16_float_vsxargs = malloc(MAX_FLOAT_VSX_ARRAY_SIZE
1495                                    * sizeof(unsigned long));
1496 
1497    for (signbit = 0; signbit < 2; signbit++) {
1498       for (exponent_index = 0; exponent_index < MAX_EXPONENTS;
1499            exponent_index++) {
1500 
1501          for (mantissa_index = 0; mantissa_index < MAX_MANTISSAS;
1502               mantissa_index++) {
1503 
1504             exponent = exponent_table[exponent_index];
1505             mantissa = mantissa_table[mantissa_index];
1506 
1507          if (verbose > 2) {
1508             printf("signbit:%lx ", signbit);
1509             printf("exponent:%4lx ", exponent);
1510             printf("mantissa:%lx ", mantissa);
1511             printf("\n");
1512          }
1513 
1514          binary128_float_vsxargs[i] = build_binary128_float(signbit, exponent,
1515                                                             mantissa);
1516 
1517          binary128_float_vsxargs[i+1] = 0;
1518 
1519          binary64_float_vsxargs[i] = build_binary64_float(signbit, exponent,
1520                                                           mantissa);
1521 
1522          binary64_float_vsxargs[i+1] = build_binary64_float(signbit, exponent,
1523                                                             mantissa);
1524 
1525          binary32_float_vsxargs[i] = build_binary32_float(signbit, exponent,
1526                                                           mantissa);
1527 
1528          binary32_float_vsxargs[i+1] = build_binary32_float(signbit, exponent,
1529                                                             mantissa);
1530 
1531          binary16_float_vsxargs[i] = build_binary16_float(signbit, exponent,
1532                                                           mantissa);
1533 
1534          binary16_float_vsxargs[i+1] = build_binary16_float(signbit, exponent,
1535                                                             mantissa);
1536          i += 2;
1537          }
1538       }
1539    }
1540    nb_float_vsxargs = i;
1541 }
1542 
1543 /* Display entries stored in the float_vsx table.  These are used as
1544  * quad/double/singles, stored as quads. */
dump_float_vsx_table(void)1545 void dump_float_vsx_table (void) {
1546    int i;
1547 
1548    printf("Float VSX Table:");
1549    printf("128-bit (quad):\n");
1550 
1551    for (i = 0; i < nb_float_vsxargs; i += 2) {
1552       printf("i =: %2d ", i);
1553       dissect_binary128_float(binary128_float_vsxargs[i]);
1554    }
1555 
1556    printf("64-bit (double):\n");
1557 
1558    for (i = 0; i< nb_float_vsxargs; i += 2) {
1559       printf("i = %2d ", i);
1560       dissect_binary64_float(binary64_float_vsxargs[i]);
1561    }
1562 
1563    printf("32-bit (single):\n");
1564 
1565    for (i = 0; i < nb_float_vsxargs; i += 2) {
1566       printf("i = %2d ", i);
1567       dissect_binary32_float(binary32_float_vsxargs[i]);
1568    }
1569 
1570    printf("16-bit (half):\n");
1571 
1572    for (i = 0; i < nb_float_vsxargs; i += 2) {
1573       printf("i =% 2d ", i);
1574       dissect_binary16_float(binary16_float_vsxargs[i]);
1575    }
1576 
1577    printf("\n");
1578 }
1579 
print_dcmx_field(unsigned long local_dcmx)1580 static void print_dcmx_field(unsigned long local_dcmx) {
1581    /* Note - this splats out the local_dxmc field from the form used to
1582     * globally pass it, with a single set bit, into the functions that use
1583     * it.  The actual DCMX field is a bit-field from 0x00 to 0x3f. If
1584     * multiple bits are ever set, this function and the way it is passed
1585     * into the users will need to be updated.  This does not handle
1586     * multiple bits being set.
1587     */
1588 
1589    printf(" DCMX=[");
1590 
1591    switch(local_dcmx) {
1592       case 0: printf("ALL"); break;
1593       case 1: printf("NaN"); break;
1594       case 2: printf("+inf"); break;
1595       case 3: printf("-inf"); break;
1596       case 4: printf("+zero"); break;
1597       case 5: printf("-zero"); break;
1598       case 6: printf("+denormal"); break;
1599       case 7: printf("-denormal"); break;
1600       default: printf("other"); break;
1601    }
1602 
1603    if (verbose > 3)
1604       printf(" %lx", local_dcmx);
1605 
1606    printf("] ");
1607 }
1608 
1609 #define MAX_CHAR_ARGS_ARRAY_SIZE 128
1610 
1611 static unsigned char * char_args;
1612 unsigned long nb_char_args;
1613 
build_char_table(void)1614 static void build_char_table(void) {
1615    long i = 0;
1616    char ichar;
1617 
1618    char_args = memalign(32, MAX_CHAR_ARGS_ARRAY_SIZE * sizeof(char));
1619 
1620 #ifdef EXHAUSTIVE_TESTS
1621    for (ichar = 'a'; ichar <= 'z'; ichar++) { char_args[i++] = ichar; }
1622    for (ichar = '0'; ichar <= '9'; ichar++) { char_args[i++] = ichar; }
1623    for (ichar = 'A'; ichar <= 'Z'; ichar++) { char_args[i++] = ichar; }
1624 #else
1625    for (ichar = 'a'; ichar <= 'z'; ichar+=6) { char_args[i++] = ichar; }
1626    for (ichar = '0'; ichar <= '9'; ichar+=6) { char_args[i++] = ichar; }
1627    for (ichar = 'A'; ichar <= 'Z'; ichar+=6) { char_args[i++] = ichar; }
1628 #endif
1629 
1630    char_args[i++] = ' ';
1631    char_args[i++] = '+';
1632    char_args[i++] = '-';
1633    char_args[i++] = '/';
1634    char_args[i++] = '[';
1635    char_args[i++] = ']';
1636    char_args[i++] = '`';
1637    char_args[i++] = '_';
1638    nb_char_args = i;
1639 }
1640 
dump_char_table()1641 static void dump_char_table() {
1642    int i;
1643 
1644    printf("Char Table:");
1645 
1646    for (i = 0; i<nb_char_args; i++)
1647       printf("%c ", char_args[i]);
1648 
1649    printf("\n");
1650 }
1651 
1652 #define MAX_CHAR_RANGES_SIZE 128
1653 
1654 static unsigned char * char_ranges;
1655 unsigned long nb_char_ranges;
1656 
build_char_range_table(void)1657 static void build_char_range_table(void) {
1658 /* ... in groups of four. */
1659 
1660    long i = 0;
1661    char char_start, char_end;
1662 
1663    char_ranges = memalign(32, MAX_CHAR_RANGES_SIZE * sizeof(char));
1664    char_start = 'a';
1665    char_end   = 'z';
1666    char_ranges[i++] = char_start;
1667    char_ranges[i++] = char_end;
1668 
1669    char_start = 'A';
1670    char_end   = 'Z';
1671    char_ranges[i++] = char_start;
1672    char_ranges[i++] = char_end;
1673 
1674    char_start = '0';
1675    char_end   = '9';
1676    char_ranges[i++] = char_start;
1677    char_ranges[i++] = char_end;
1678 
1679    char_start = 'f';
1680    char_end   = 'z';
1681    char_ranges[i++] = char_start;
1682    char_ranges[i++] = char_end;
1683 
1684    char_start = 'a';
1685    char_end   = 'e';
1686    char_ranges[i++] = char_start;
1687    char_ranges[i++] = char_end;
1688 
1689    char_start = 'A';
1690    char_end   = 'E';
1691    char_ranges[i++] = char_start;
1692    char_ranges[i++] = char_end;
1693 
1694    nb_char_ranges = i;
1695 }
1696 
dump_char_range_table()1697 static void dump_char_range_table()
1698 {
1699    int i;
1700 
1701    printf("Char Range Table:");
1702 
1703    for (i = 0; i < nb_char_ranges; i += 4) {
1704        printf(" [ %c-%c %c-%c ] ",
1705                 char_ranges[i], char_ranges[i+1],
1706                 char_ranges[i+2], char_ranges[i+3] );
1707    }
1708 
1709    printf("\n");
1710 }
1711 
1712 static HWord_t *iargs = NULL;
1713 static int nb_iargs = 0;
1714 
build_iargs_table(void)1715 static void build_iargs_table (void) {
1716    uint64_t tmp;
1717    int i = 0;
1718 
1719    iargs = malloc(20 * sizeof(HWord_t));
1720 
1721    for (tmp = 0; ; tmp = 123456789*tmp + 123456789999) {
1722       if ((long)tmp < 0 )
1723          tmp = 0xFFFFFFFFFFFFFFFFULL;
1724 
1725       iargs[i++] = tmp;
1726       AB_DPRINTF("val %016lx\n", tmp);
1727 
1728       if (tmp == 0xFFFFFFFFFFFFFFFFULL)
1729          break;
1730    }
1731 
1732    AB_DPRINTF("Registered %d iargs values\n", i);
1733    nb_iargs = i;
1734 }
1735 
1736 static unsigned long * vsxargs = NULL;
1737 unsigned long nb_vargs;
1738 
1739 #define MAX_VSX_ARRAY_SIZE 42
1740 
build_vsx_table(void)1741 static void build_vsx_table (void)
1742 {
1743    long i = 0;
1744    // A VSX register is 128-bits wide.
1745    // We build contents here using pairs of 64-bit longs.
1746    // Permutes work against two (non-paired) VSX regs, so these are
1747    //  also grouped by twos.
1748    vsxargs = memalign(16, MAX_VSX_ARRAY_SIZE * sizeof(unsigned long));
1749 #ifdef EXHAUSTIVE_TESTS
1750    vsxargs[i++] = 0x0000000000000000UL; vsxargs[i++] = 0x0000000000000000UL;
1751    vsxargs[i++] = 0x0102030405060708UL; vsxargs[i++] = 0x0102010201020102UL;
1752 
1753    vsxargs[i++] = 0xaaaaaaaaaaaaaaaaUL; vsxargs[i++] = 0xaaaaaaaaaaaaaaaaUL;
1754    vsxargs[i++] = 0x5555555555555555UL; vsxargs[i++] = 0x5555555555555555UL;
1755 
1756    vsxargs[i++] = 0x08090a0b0c0d0e0fUL; vsxargs[i++] = 0x0102010201020102UL;
1757    vsxargs[i++] = 0xf0f1f2f3f4f5f6f7UL; vsxargs[i++] = 0xf8f9fafbfcfdfeffUL;
1758 
1759    vsxargs[i++] = 0x7ea1a5a7abadb0baUL; vsxargs[i++] = 0x070d111d1e555e70UL;
1760    vsxargs[i++] = 0xe5e7ecedeff0f1faUL; vsxargs[i++] = 0xbeb1c0caced0dbdeUL;
1761 
1762    vsxargs[i++] = 0x00115e7eadbabec0UL; vsxargs[i++] = 0xced0deede5ecef00UL;
1763    vsxargs[i++] = 0x00111e7ea5abadb1UL; vsxargs[i++] = 0xbecad0deedeffe00UL;
1764 
1765    vsxargs[i++] = 0x0011223344556677UL; vsxargs[i++] = 0x8899aabbccddeeffUL;
1766    vsxargs[i++] = 0xf0e0d0c0b0a09080UL; vsxargs[i++] = 0x7060504030201000UL;
1767 #else
1768    vsxargs[i++] = 0x0000000000000000UL; vsxargs[i++] = 0x0000000000000000UL;
1769    vsxargs[i++] = 0x0102030405060708UL; vsxargs[i++] = 0x0102010201020102UL;
1770 
1771    vsxargs[i++] = 0x0011223344556677UL; vsxargs[i++] = 0x8899aabbccddeeffUL;
1772    vsxargs[i++] = 0xf0e0d0c0b0a09080UL; vsxargs[i++] = 0x7060504030201000UL;
1773 #endif
1774 
1775    // these next three groups are specific for vector rotate tests.
1776    //  bits 11:15,19:23,27:31 of each 32-bit word contain mb,me,sh values.
1777    vsxargs[i++] = 0x0000100000001002ULL; vsxargs[i++] = 0x0000100800001010ULL;
1778    vsxargs[i++] = 0x0010100000101002ULL; vsxargs[i++] = 0x0010100800101010ULL;
1779 
1780    // vector rotate special...
1781    vsxargs[i++] = 0x00001c0000001c02ULL; vsxargs[i++] = 0x00001c0800001c10ULL;
1782    vsxargs[i++] = 0x00101c0000101c02ULL; vsxargs[i++] = 0x00101c0800101c10ULL;
1783 
1784    // vector rotate special...
1785    vsxargs[i++] = 0x00001f0000001f02ULL; vsxargs[i++] = 0x00001f0800001f10ULL;
1786    vsxargs[i++] = 0x00101f0000101f02ULL; vsxargs[i++] = 0x00101f0800101f10ULL;
1787 
1788    AB_DPRINTF("Registered %d vargs values\n", i/2);
1789    nb_vargs = i;
1790 }
1791 
1792 /* VPCV = Vector Permute Control Vector */
1793 unsigned long nb_vpcv;
1794 static unsigned long * vpcv = NULL;
1795 
1796 #define MAX_VPCV_SIZE 20
1797 
build_vector_permute_table(void)1798 static void build_vector_permute_table(void)
1799 {
1800    int i=0;
1801 
1802    vpcv = memalign(16, MAX_VPCV_SIZE * sizeof(unsigned long));
1803 
1804 #ifdef EXHAUSTIVE_TESTS
1805    /* These two lines are complementary pairs of each other. */
1806    vpcv[i++]=0x12021a0817141317ULL; vpcv[i++]=0x100d1b05070f0205ULL;
1807    vpcv[i++]=0x0d1d0517080b0c08ULL; vpcv[i++]=0x0f12041a18101d1cULL;
1808    vpcv[i++]=0x100d1b070f020505ULL; vpcv[i++]=0x0e201f1400130105ULL;
1809    vpcv[i++]=0x0705030a0b01ea0cULL; vpcv[i++]=0x0e0c09010602080dULL;
1810 #else
1811    vpcv[i++]=0x12021a0817141317ULL; vpcv[i++]=0x100d1b05070f0205ULL;
1812    vpcv[i++]=0x0705030a0b01ea0cULL; vpcv[i++]=0x0e0c09010602080dULL;
1813 #endif
1814    nb_vpcv=i;
1815    AB_DPRINTF("Registered %d permute control vectors \n", nb_vpcv);
1816 
1817    if (i >= MAX_VPCV_SIZE)
1818       printf("Warning! Exceeded size of table building the vector permute control . \n");
1819 }
1820 
1821 /* Decimal Encodings...
1822  * Packed, National, Zoned decimal content follows.
1823  * Note: Watch the conversions in and out of the
1824  *       dwords / vectors for reverses with respect to
1825  *       top/bottom low/high
1826  */
1827 
1828 /* Packed Decimals:
1829  * A valid encoding of a packed decimal integer value requires the following
1830  * properties:
1831  *   – Each of the 31 4-bit digits of the operand’s magnitude (bits 0:123)
1832  *     must be in the range 0-9.
1833  *   – The sign code (bits 124:127) must be in the range 10-15. (0xa-0xf).
1834  * Source operands with sign codes of 0b1010, 0b1100, 0b1110, and 0b1111 are
1835  * interpreted as positive values.  Source operands with sign codes of
1836  * 0b1011 and 0b1101 are interpreted as negative values.
1837  * Positive and zero results are encoded with a either sign code of
1838  * 0b1100 or 0b1111, depending on the preferred sign (indicated as an
1839  * immediate operand).  Negative results are encoded with a sign code
1840  * of 0b1101.
1841  * PS - This is the 'preferred sign' bit encoded in some BCD associated
1842  * instructions.
1843  */
1844 
1845 // Note: table content is limited to values encoded, not interpreted.
1846 unsigned int packed_decimal_sign_codes[] = {
1847    /* positive operands */
1848    0xc, 0xf,  // 0b1100, 0b1111
1849 
1850    /* negative operands */
1851    0xd  // 0b1101
1852 };
1853 
1854 #define NR_PACKED_DECIMAL_SIGNS 3
1855 #define MAX_PACKED_DECIMAL_TABLE_SIZE 8 * 16 * 2 + 20
1856 
1857 static unsigned long * packed_decimal_table;
1858 
1859 /* build into a pair of doubles */
1860 unsigned long nb_packed_decimal_entries;
1861 
dissect_packed_decimal_sign(unsigned long local_sign)1862 static void dissect_packed_decimal_sign(unsigned long local_sign) {
1863   switch(local_sign) {
1864      case 0xa: /*0b1010:*/ printf("[ + ]"); break;
1865      case 0xb: /*0b1011:*/ printf("[ - ]"); break;
1866      case 0xc: /*0b1100:*/ printf("(+|0)"); break;
1867      case 0xd: /*0b1101:*/ printf("( - )"); break;
1868      case 0xe: /*0b1110:*/ printf("[ + ]"); break;
1869      case 0xf: /*0b1111:*/ printf("(+|0)"); break;
1870      default: printf("(?%02lx)", local_sign);
1871   }
1872 }
1873 
extract_packed_decimal_sign(unsigned long dword1,unsigned long dword0)1874 int extract_packed_decimal_sign(unsigned long dword1, unsigned long dword0) {
1875    return  (dword1 & 0xf);
1876 }
1877 
dissect_packed_decimal(unsigned long dword1,unsigned long dword0)1878 static void dissect_packed_decimal(unsigned long dword1,unsigned long dword0)
1879 {
1880    int i;
1881    int local_sign;
1882    int nibble;
1883 
1884    local_sign = extract_packed_decimal_sign(dword1, dword0);
1885    printf("packed_decimal: [");
1886 
1887    for (i = 60; i >= 0; i -= 4) {
1888       nibble=(dword1 >> (i)) & 0xf;
1889       printf(" %x", nibble);
1890    }
1891 
1892    for (i = 60; i >= 0; i -= 4) {
1893       nibble=(dword0 >> (i)) & 0xf;
1894       printf(" %x", nibble);
1895    }
1896 
1897    printf(" ");
1898    dissect_packed_decimal_sign(local_sign);
1899    printf(" ] ");
1900 }
1901 
build_packed_decimal_table(void)1902 static void build_packed_decimal_table(void)
1903 {
1904    long sign_index;
1905    long sign_value;
1906    unsigned long i = 0;
1907    unsigned long value;
1908  #ifdef EXHAUSTIVE_TESTS
1909    int scramble;
1910 #endif
1911 
1912    if (verbose) printf("%s\n", __FUNCTION__);
1913 
1914    packed_decimal_table = malloc((MAX_PACKED_DECIMAL_TABLE_SIZE + 2)
1915                                  * sizeof (unsigned long));
1916 
1917    for (sign_index = 0; sign_index < NR_PACKED_DECIMAL_SIGNS; sign_index++) {
1918       sign_value = packed_decimal_sign_codes[sign_index];
1919 
1920       for (value = 0; value <= 9; value++) {
1921         packed_decimal_table[i]    = 0x1111111111111111 * value;
1922         packed_decimal_table[i+1]  = sign_value;
1923         packed_decimal_table[i+1] += 0x1111111111111110 * value;
1924 
1925         if (verbose>3) dissect_packed_decimal(packed_decimal_table[i+1],
1926                                               packed_decimal_table[i]);
1927         if (verbose>3) printf("\n");
1928         i+=2;
1929       }
1930 
1931 #ifdef EXHAUSTIVE_TESTS
1932       for (scramble = 1; scramble <= 4; scramble++) {
1933         packed_decimal_table[i]    = 0x3210321032103210 * scramble;
1934         packed_decimal_table[i+1]  = sign_value;
1935         packed_decimal_table[i+1] += 0x0123012301230120 * scramble;
1936 
1937         if (verbose>3) dissect_packed_decimal(packed_decimal_table[i+1],
1938                                               packed_decimal_table[i]);
1939         if (verbose>3) printf("\n");
1940         i+=2;
1941       }
1942 #endif
1943 
1944       /* Add some entries that will provide interesting output from
1945        * the convert TO tests.
1946        */
1947       packed_decimal_table[i]    = 0x0000000000000000;
1948       packed_decimal_table[i+1]  = sign_value;
1949       packed_decimal_table[i+1] += 0x0000000012345670;
1950 
1951       if (verbose > 3) dissect_packed_decimal(packed_decimal_table[i+1],
1952                                               packed_decimal_table[i]);
1953 
1954       if (verbose>3) printf("\n");
1955 
1956       i += 2;
1957 
1958 #ifdef EXHAUSTIVE_TESTS
1959       packed_decimal_table[i]    = 0x0000000000000000;
1960       packed_decimal_table[i+1]  = sign_value;
1961       packed_decimal_table[i+1] += 0x0000000098765430;
1962 
1963       if (verbose > 3) dissect_packed_decimal(packed_decimal_table[i+1],
1964                                               packed_decimal_table[i]);
1965 
1966       if (verbose > 3) printf("\n");
1967 
1968       i += 2;
1969 
1970       packed_decimal_table[i]    = 0x000000000000000b;
1971       packed_decimal_table[i+1]  = sign_value;
1972       packed_decimal_table[i+1] += 0x0000000000000000;
1973 
1974       if (verbose > 3) dissect_packed_decimal(packed_decimal_table[i+1],
1975                                               packed_decimal_table[i]);
1976 
1977       if (verbose>3) printf("\n");
1978 
1979       i += 2;
1980 #endif
1981 
1982       packed_decimal_table[i]    = 0x0030000000000000;
1983       packed_decimal_table[i+1]  = sign_value;
1984       packed_decimal_table[i+1] += 0x0000000000000000;
1985 
1986       if (verbose > 3) dissect_packed_decimal(packed_decimal_table[i+1],
1987                                             packed_decimal_table[i]);
1988 
1989       if (verbose > 3) printf("\n");
1990 
1991       i += 2;
1992    }
1993 
1994    if (verbose>2) printf("\n");
1995 
1996    nb_packed_decimal_entries = i;
1997 }
1998 
dump_packed_decimal_table(void)1999 static void dump_packed_decimal_table(void) {
2000    int i;
2001 
2002    printf("packed_decimal_table:\n");
2003 
2004    for (i = 0; i < nb_packed_decimal_entries; i += 2) {
2005       printf("i =: %2d ", i);
2006       dissect_packed_decimal(packed_decimal_table[i+1],
2007                              packed_decimal_table[i]);
2008       printf("\n");
2009    }
2010 }
2011 
2012 /* National decimals:
2013  * A valid encoding of a national decimal value requires the following.
2014  * – The contents of halfword 7 (sign code) must be
2015  *   either 0x002B or 0x002D.
2016  * – The contents of halfwords 0 to 6 must be in the
2017  *   range 0x0030 to 0x0039.
2018  * National decimal values having a sign code of 0x002B
2019  * are interpreted as positive values.
2020  * National decimal values having a sign code of 0x002D
2021  * are interpreted as negative values.
2022  */
2023 unsigned int national_decimal_sign_codes[] = {
2024    /* positive */  0x002b,
2025    /* negative */  0x002d
2026 };
2027 
2028 #define NR_NATIONAL_DECIMAL_SIGNS 2
2029 
2030 unsigned int national_decimal_values[] = {
2031 #ifdef EXHAUSTIVE_TESTS
2032    0x0030, 0x0031, 0x0032, 0x0033, 0x0034,
2033    0x0035, 0x0036, 0x0037, 0x0038, 0x0039
2034 #else
2035    0x0030, 0x0031,
2036    0x0035, 0x0039
2037 #endif
2038 };
2039 
2040 #define NR_NATIONAL_DECIMAL_VALUES (sizeof(national_decimal_values) / sizeof(unsigned int))
2041 
2042 static unsigned long * national_decimal_table;
2043 
2044 #define MAX_NATIONAL_DECIMAL_TABLE_SIZE 10 * NR_NATIONAL_DECIMAL_VALUES * NR_NATIONAL_DECIMAL_SIGNS
2045 
2046 unsigned long nb_national_decimal_entries;
2047 
dissect_national_decimal_sign(unsigned long local_sign)2048 static void dissect_national_decimal_sign(unsigned long local_sign) {
2049    switch(local_sign) {
2050       case 0x002b:
2051             printf("( + )");
2052             break;
2053 
2054       case 0x002d:
2055             printf("( - )");
2056             break;
2057 
2058      default: printf("unhandled sign value: %lx", local_sign);
2059    }
2060 }
2061 
extract_national_decimal_sign(unsigned long dword1,unsigned long dword0)2062 int extract_national_decimal_sign(unsigned long dword1, unsigned long dword0) {
2063    return (dword1 & 0x0ff);
2064 }
2065 
dissect_national_decimal(unsigned long dword1,unsigned long dword0)2066 static void dissect_national_decimal(unsigned long dword1,
2067                                      unsigned long dword0)
2068 {
2069    int i;
2070    int local_sign;
2071    long hword;
2072 
2073    printf("national_decimal: [");
2074 
2075    if (verbose>4) printf("raw: [%016lx %016lx] ", dword1, dword0);
2076 
2077    for (i = 48;i >= 0; i -= 16) {
2078       hword = dword1 >> (i) & 0x00ff;
2079 
2080       /* validity of national decimal value */
2081       /* the i>0 clause skips the validity check against the sign value. */
2082       if (((i > 0) && (hword < 0x30)) || (hword > 0x39)) printf("!");
2083 
2084       printf("%04lx ", hword);
2085    }
2086 
2087    for (i = 48; i >= 0; i -= 16) {
2088       hword = dword0 >> (i) & 0x00ff;
2089 
2090       if ((hword < 0x30) || (hword > 0x39)) printf("!");
2091 
2092       printf("%04lx ", hword);
2093    }
2094 
2095    local_sign = extract_national_decimal_sign(dword1, dword0);
2096    dissect_national_decimal_sign(local_sign);
2097    printf(" ] ");
2098 }
2099 
build_national_decimal_table(void)2100 static void build_national_decimal_table(void)
2101 {
2102    long sign_index;
2103    long sign_value;
2104    unsigned long i = 0;
2105    int index;
2106    unsigned long value;
2107 
2108    if (verbose) printf("%s\n",__FUNCTION__);
2109    national_decimal_table = malloc(MAX_NATIONAL_DECIMAL_TABLE_SIZE
2110                                    * sizeof (unsigned long));
2111 
2112    for (sign_index = 0; sign_index < NR_NATIONAL_DECIMAL_SIGNS; sign_index++) {
2113       sign_value = national_decimal_sign_codes[sign_index];
2114 
2115       for (index = 0; index < NR_NATIONAL_DECIMAL_VALUES; index++) {
2116          value = national_decimal_values[index];
2117 
2118          national_decimal_table[i]    = 0x0001000100010001 * value;
2119          national_decimal_table[i+1]  = 0x0001000100010000 * value;
2120          national_decimal_table[i+1] += sign_value ;
2121 
2122          if (verbose > 3) {
2123             dissect_national_decimal(national_decimal_table[i+1],
2124                                      national_decimal_table[i]);
2125             printf("\n");
2126          }
2127          i += 2;
2128       }
2129 #ifdef EXHAUSTIVE_TESTS
2130       { /* a few more for fun */
2131          national_decimal_table[i]    = 0x0031003200330034;
2132          national_decimal_table[i+1]  = 0x0035003600370000;
2133          national_decimal_table[i+1] += sign_value ;
2134 
2135          if (verbose > 3) {
2136             dissect_national_decimal(national_decimal_table[i+1],
2137                                      national_decimal_table[i]);
2138             printf("\n");
2139          }
2140 
2141          i += 2;
2142          national_decimal_table[i]    = 0x0031003200330034;
2143          national_decimal_table[i+1]  = 0x0035003600370000;
2144          national_decimal_table[i+1] += sign_value ;
2145 
2146          if (verbose > 3) {
2147             dissect_national_decimal(national_decimal_table[i+1],
2148                                      national_decimal_table[i]);
2149             printf("\n");
2150          }
2151          i += 2;
2152       }
2153 #endif
2154    }
2155 
2156    if (verbose > 2) printf("\n");
2157 
2158    nb_national_decimal_entries = i;
2159 }
2160 
dump_national_decimal_table(void)2161 static void dump_national_decimal_table(void) {
2162    int i;
2163 
2164    printf("national_decimal_table:\n");
2165 
2166    for (i = 0; i < nb_national_decimal_entries; i += 2) {
2167       printf("#%2d ", i);
2168       dissect_national_decimal(national_decimal_table[i+1],
2169                                national_decimal_table[i]);
2170       printf("\n");
2171    }
2172 }
2173 
2174 
2175 /* Zoned Decimals:
2176  *
2177  * When PS=0, do the following.
2178  *  A valid encoding of a zoned decimal value requires the following.
2179  *  – The contents of bits 0:3 of byte 15 (sign code) can be any
2180  *    value in the range 0x0 to 0xF.
2181  *  – The contents of bits 0:3 of bytes 0 to 14 (zone) must
2182  *    be the value 0x3.
2183  *  – The contents of bits 4:7 of bytes 0 to 15 must
2184  *    be a value in the range 0x0 to 0x9.
2185  *  Zoned decimal values having a sign code of 0x0, 0x1, 0x2, 0x3,
2186  *  0x8, 0x9, 0xA, or 0xB are interpreted as positive values.
2187  *  Zoned decimal values having a sign code of 0x4, 0x5, 0x6, 0x7,
2188  *  0xC, 0xD, 0xE, or 0xF are interpreted as negative values.
2189     :: 0,1,2,3,        8,9,a,b,         are interpreted as positive.
2190     ::         4,5,6,7,        c,d,e,f  are interpreted as negative.
2191  * When PS=1, do the following.
2192  *  A valid encoding of a zoned decimal source operand requires the following.
2193  *  – The contents of bits 0:3 of byte 15 (sign code) must be a value in the
2194  *    range 0xA to 0xF.
2195  *  – The contents of bits 0:3 of bytes 0 to 14 (zone) must be the value 0xF.
2196  *  – The contents of bits 4:7 of bytes 0 to 15 must be a value in the
2197  *    range 0x0 to 0x9.
2198  *  Zoned decimal source operands having a sign code of 0xA, 0xC, 0xE,
2199  *  or 0xF are interpreted as positive values.
2200  *  Zoned decimal source operands having a sign code of 0xB or 0xD are
2201  *  interpreted as negative values.
2202     ::                     a,  c,  e,f  are interpreted as positive.
2203     ::                       b,  d,     are interpreted as negative.
2204  */
2205 
2206 /* a valid sign is anything in range 0-9,a-f,
2207  * For coverage that does not overwhelm, we have chosen to use  0,1,4,a,b,f. */
2208 #define NM_ZONED_DECIMAL_SIGNS 6
2209 #define NM_ZONED_VALUES 5   /* 0,2,4,6,9 */
2210 #define NM_PS_VALUES 2      /* 0,1 */
2211 #define NM_ZONED_ADDITIONAL_PATTERNS 4
2212 #define MAX_ZONED_DECIMAL_TABLE_SIZE  NM_ZONED_DECIMAL_SIGNS * NM_ZONED_VALUES * NM_ZONED_ADDITIONAL_PATTERNS * NM_PS_VALUES + 10
2213 
2214 static unsigned long zoned_decimal_table_[MAX_ZONED_DECIMAL_TABLE_SIZE];
2215 static unsigned long * zoned_decimal_table;
2216 unsigned long nb_zoned_decimal_entries;
2217 
dissect_zoned_decimal_sign(unsigned long local_sign,int ps)2218 static void dissect_zoned_decimal_sign(unsigned long local_sign, int ps) {
2219    if (ps == 0) {
2220       switch(local_sign) {
2221          case 0x0: case 0x1: case 0x2: case 0x3:
2222          case 0x8: case 0x9: case 0xa: case 0xb:
2223             printf("( + )");
2224             break;
2225 
2226          case 0x4: case 0x5: case 0x6: case 0x7:
2227          case 0xc: case 0xd: case 0xe: case 0xf:
2228             printf("( - )");
2229             break;
2230          default: printf("zoned decimal (ps=%d). Unhandled sign value: %lx",
2231                          ps, local_sign);
2232       }
2233    }
2234 
2235    if (ps == 1) {
2236       switch(local_sign) {
2237          case 0xa: case 0xc: case 0xe: case 0xf:
2238             printf("( + )");
2239             break;
2240 
2241          case 0xb: case 0xd:
2242             printf("( - )");
2243             break;
2244 
2245          default: printf("zoned decimal (ps=%d). Unhandled sign value: %lx",
2246                          ps, local_sign);
2247       }
2248    }
2249 }
2250 
2251 /* Valid byte values within a zoned decimal are in the ranges of
2252  * 0x30..0x39 when PS==0, or 0xf0..0xff when PS==1.
2253  */
check_zoned_byte_validity(int byte,int ps)2254 static void check_zoned_byte_validity(int byte, int ps) {
2255    if (ps == 0) {
2256       /* check the zone */
2257       if (((byte & 0x30) != 0x30))
2258          printf("!=30");
2259 
2260    } else { /* ps==1 */
2261       if (((byte & 0xf0) != 0xf0))
2262          printf("%x !=f0 ", byte );
2263    }
2264 
2265    /* check the numeric value */
2266    if ((byte & 0x0f) > 0x9)
2267       printf("!(0..9)");
2268 }
2269 
extract_zoned_decimal_sign(unsigned long dword1,unsigned long dword0)2270 int extract_zoned_decimal_sign(unsigned long dword1, unsigned long dword0) {
2271    return ((dword1 & 0xf0) >> 4);
2272 }
2273 
dissect_zoned_decimal(unsigned long dword1,unsigned long dword0,int ps)2274 static void dissect_zoned_decimal(unsigned long dword1, unsigned long dword0,
2275                                   int ps)
2276 {
2277    int i;
2278    int local_sign;
2279    int byte;
2280 
2281    printf("zoned_decimal: [");
2282 
2283    for (i = 56; i >= 0; i -= 8) {
2284       byte = (dword1 >> (i)) & 0xff;
2285       check_zoned_byte_validity(byte, ps);
2286       printf(" %02x", byte);
2287    }
2288 
2289    for (i = 56; i >= 0; i -= 8) {
2290       byte = (dword0 >> (i)) & 0x00ff;
2291       check_zoned_byte_validity(byte, ps);
2292 
2293       if ((byte & 0xf) > 0x9) printf(" !(>9)");
2294       printf(" %02x", byte);
2295    }
2296 
2297    local_sign = extract_zoned_decimal_sign(dword1, dword0);
2298    dissect_zoned_decimal_sign(local_sign, ps);
2299    printf(" ]");
2300 }
2301 
2302 #ifdef EXHAUSTIVE_TESTS
2303 // Randomly chosen exhaustive coverage for k includes values: 0,2,4,7,9
2304 # define SELECTIVE_INCREMENT_ZONED(k) \
2305    if (k == 7) k = 9;                    \
2306       else if (k == 4) k = 7;            \
2307          else if (k == 2) k = 4;         \
2308             else if (k == 0) k = 2;      \
2309                else k++;
2310 // Randomly chosen exhaustive coverage for signs includes values: 0,1,4,a,b,f
2311 # define SELECTIVE_INCREMENT_SIGNS(signs)              \
2312          if (signs == 0x0) signs = 0x1;                   \
2313          else if (signs == 0x1) signs = 0x4;              \
2314             else if (signs == 0x4) signs = 0xa;           \
2315                else if (signs == 0xa) signs = 0xb;        \
2316                   else if (signs == 0xb) signs = 0xf;     \
2317                      else signs++;
2318 #else
2319 // Randomly chosen coverage for k includes values: 0,7,9
2320 # define SELECTIVE_INCREMENT_ZONED(k) \
2321    if (k == 7) k = 9;                 \
2322       else if (k == 0) k = 7;      \
2323          else k++;
2324 // Randomly chosen coverage for signs includes values: 0,4,b,f
2325 # define SELECTIVE_INCREMENT_SIGNS(signs)                  \
2326       if (signs == 0x0) signs = 0x4;                      \
2327             else if (signs == 0x4) signs = 0xb;           \
2328                   else if (signs == 0xb) signs = 0xf;     \
2329                      else signs++;
2330 #endif
2331 
2332 
build_zoned_decimal_table(void)2333 static void build_zoned_decimal_table(void)
2334 {
2335    unsigned long signs;
2336    unsigned long i;
2337    int k;
2338    int ps;
2339    int signs_start,signs_end;
2340 
2341    if (verbose) printf("%s\n", __FUNCTION__);
2342 
2343    zoned_decimal_table = zoned_decimal_table_;
2344    i = 0;
2345 
2346    for (ps = 0; ps <= 1; ps++) {
2347       if (ps == 0) {
2348          signs_start = 0;
2349          signs_end   = 0xf;
2350 
2351       } else {
2352          signs_start = 0xa;
2353          signs_end   = 0xf;
2354       }
2355 
2356       for (signs = signs_start;
2357            signs <= signs_end;  /* signs selectively updated below */) {
2358 
2359          if (verbose > 2) printf("ps=%d sign:%lx\n", ps, signs);
2360 
2361          for (k = 0 ; k < 9;  /* k selectively updated below */) {
2362             if (ps == 0) {
2363                zoned_decimal_table[i]   = 0x3030303030303030;  // set bits 0:3 of bytes 0..7.
2364                zoned_decimal_table[i+1] = 0x3030303030303000;  // bits 0:3 of bytes 8..14 must be 0x3
2365 
2366             } else {
2367                zoned_decimal_table[i]   = 0xf0f0f0f0f0f0f0f0;  // set bits 0:3 of bytes 0..7.
2368                zoned_decimal_table[i+1] = 0xf0f0f0f0f0f0f000;  // bits 0:3 of bytes 8..14 must be 0x3
2369             }
2370 
2371             zoned_decimal_table[i]   += 0x010101010101010 * k; // set bits 4..7 of bytes 0..7.
2372             zoned_decimal_table[i+1] += 0x010101010101000 * k; // bits 4:7 of bytes 8..15 must be 0..9.
2373             zoned_decimal_table[i+1] += (signs << 4); // bits 0:3 of byte 15 is the sign.
2374             if (verbose > 3) {
2375                dissect_zoned_decimal(zoned_decimal_table[i+1],
2376                                      zoned_decimal_table[i], ps);
2377                printf("\n");
2378             }
2379             i += 2;
2380             SELECTIVE_INCREMENT_ZONED(k)
2381          }
2382 
2383          /* add a few more patterns outside of the k patterns. */
2384          if (ps == 0) {
2385             zoned_decimal_table[i]   = 0x3030303030303030;
2386             zoned_decimal_table[i+1] = 0x3030303030303000;
2387 
2388          } else  {
2389             zoned_decimal_table[i]   = 0xf0f0f0f0f0f0f0f0;
2390             zoned_decimal_table[i+1] = 0xf0f0f0f0f0f0f000;
2391          }
2392 
2393          zoned_decimal_table[i]      += 0x0908070605040302;
2394          zoned_decimal_table[i+1]    += 0x0102030405060700;
2395          zoned_decimal_table[i+1]    += (signs<<4); // bits 0:3 of byte 15.
2396 
2397          if (verbose > 3) {
2398             dissect_zoned_decimal(zoned_decimal_table[i+1],
2399                                   zoned_decimal_table[i], ps);
2400             printf("\n");
2401          }
2402 
2403          i += 2;
2404          SELECTIVE_INCREMENT_SIGNS(signs)
2405       } /* signs loop */
2406    } /* ps loop */
2407 
2408    nb_zoned_decimal_entries = i;
2409 }
2410 
dump_zoned_decimal_table(void)2411 static void dump_zoned_decimal_table(void) {
2412    int i;
2413    int ps;
2414 
2415    for (ps = 0; ps <= 1; ps++) {
2416       printf("zoned_decimal_table ps=%d:\n", ps);
2417 
2418       for (i = 0; i < nb_zoned_decimal_entries; i += 2) {
2419          printf("#%2d ", i);
2420          dissect_zoned_decimal(zoned_decimal_table[i+1],
2421                                zoned_decimal_table[i], ps);
2422          printf("\n");
2423       }
2424    }
2425 }
2426 
2427 /* Build table containing shift and truncate values */
2428 #define MAX_DECIMAL_SHIFT_TABLE_SIZE 64
2429 
2430 static unsigned long * decimal_shift_table;
2431 unsigned long nb_decimal_shift_entries;
2432 
build_decimal_shift_table(void)2433 static void build_decimal_shift_table(void) {
2434    unsigned long i = 0;
2435    unsigned long value;
2436 
2437    if (verbose) printf("%s\n",__FUNCTION__);
2438 
2439    decimal_shift_table = malloc(MAX_DECIMAL_SHIFT_TABLE_SIZE
2440                                 * sizeof (unsigned long));
2441 
2442    for (value = 0; value <= 31; value++) {
2443         decimal_shift_table[i]   = value;
2444         decimal_shift_table[i+1] = 0;
2445         i += 2;
2446    }
2447 
2448    if (verbose>2) printf("\n");
2449 
2450    nb_decimal_shift_entries = i;
2451 }
2452 
dump_decimal_shift_table(void)2453 static void dump_decimal_shift_table(void) {
2454    int i;
2455 
2456    printf("decimal_shift_table:\n");
2457 
2458    for (i = 0; i < nb_decimal_shift_entries; i += 2) {
2459       printf("i=:%2d ", i);
2460       printf(" 0x%2lx 0x%2lx ", decimal_shift_table[i],
2461              decimal_shift_table[i+1]);
2462       printf("\n");
2463    }
2464 }
2465