1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
/* { dg-do run } */
/* { dg-require-effective-target int128 } */
/* { dg-require-effective-target power10_hw } */
/* { dg-options "-mdejagnu-cpu=power10 -O2 -save-temps" } */
/* { dg-final { scan-assembler-times {\mbcdadd\M} 7 } } */
/* { dg-final { scan-assembler-times {\mbcdsub\M} 18 } } */
/* { dg-final { scan-assembler-times {\mbcds\M} 2 } } */
/* { dg-final { scan-assembler-times {\mdenbcdq\M} 1 } } */
#include <altivec.h>
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#endif
#define BCD_POS0 12 // 0xC
#define BCD_POS1 15 // 0xF
#define BCD_NEG 13 // 0xD
void abort (void);
union conv_t
{
_Decimal128 d128;
vector unsigned char ch;
vector long long unsigned int vllui;
} conv;
_Decimal128 convert_vec_char (vector unsigned char a)
{
union conv_t conv;
_Decimal128 result;
conv.ch = a;
result = conv.d128;
return result;
}
vector unsigned char maxbcd(unsigned int sign)
{
vector unsigned char result;
int i;
for (i = 15; i > 0; i--)
result[i] = 0x99;
result[0] = sign << 4 | 0x9;
}
vector unsigned char num2bcd(long int a, int encoding)
{
int i;
unsigned int hi, low, sign;
vector unsigned char result;
if (a > 0) {
if (encoding == 0)
sign = BCD_POS0;
else
sign = BCD_POS1;
} else {
sign = BCD_NEG;
a = -a;
}
hi = a % 10; // 1st digit
a = a / 10;
result[0] = hi << 4| sign;
for (i = 1; i < 16; i++)
{
low = a % 10;
a = a / 10;
hi = a % 10;
a = a / 10;
result[i] = hi << 4 | low;
}
return result;
}
int main ()
{
int i;
long int value_a, value_b, value_result;
vector unsigned char a, b, result, exp_result;
_Decimal128 result_d128, exp_result_d128;
/* Make a and b positive BCD numbers */
value_a = 1020304;
a = num2bcd(value_a, 0);
value_b = 101010;
b = num2bcd(value_b, 0);
value_result = value_a + value_b;
exp_result = num2bcd(value_result, 0);
result = __builtin_bcdadd (a, b, 0);
for (i = 0; i < 16; i++)
if (exp_result[i] != result[i]) {
#if DEBUG
printf("ERROR: __builtin_bcdadd result[%d] = %d does not match "
"expected_result[%d] = %d\n",
i, result[i], i, exp_result[i]);
#else
abort();
#endif
}
/* result should be positive */
if ((result[0] & 0xF) != BCD_POS0)
#if DEBUG
printf("ERROR: __builtin_bcdadd sign of result is %d. Does not match "
"expected_result = %d\n",
result[0] & 0xF, BCD_POS0);
#else
abort();
#endif
/* Make a and b positive BCD numbers using alternate positive encoding. */
value_a = 1030507;
a = num2bcd(value_a, 1);
value_b = 204060;
b = num2bcd(value_b, 1);
value_result = value_a + value_b;
exp_result = num2bcd(value_result, 1);
result = __builtin_bcdadd (a, b, 1);
for (i = 0; i < 16; i++)
if (exp_result[i] != result[i]) {
#if DEBUG
printf("ERROR: __builtin_bcdadd result[%d] = %d does not match "
"expected_result[%d] = %d\n",
i, result[i], i, exp_result[i]);
#else
abort();
#endif
}
/* Result should be positive, alternate encoding. */
if ((result[0] & 0xF) != BCD_POS1)
#if DEBUG
printf("ERROR: __builtin_bcdadd sign of result is %d. Does not "
"match expected_result = %d\n",
result[0] & 0xF, BCD_POS1);
#else
abort();
#endif
/* Make a and b negative BCD numbers */
value_a = -1030507;
a = num2bcd(value_a, 0);
value_b = -1010101;
b = num2bcd(value_b, 0);
value_result = value_a + value_b;
exp_result = num2bcd(value_result, 0);
result = __builtin_bcdadd (a, b, 0);
for (i = 0; i < 16; i++)
if (exp_result[i] != result[i]) {
#if DEBUG
printf("ERROR: __builtin_bcdadd, neg result[%d] = %d does not match "
"expected_result[%d] = %d\n",
i, result[i], i, exp_result[i]);
#else
abort();
#endif
}
/* result should be negative */
if ((result[0] & 0xF) != BCD_NEG)
#if DEBUG
printf("ERROR: __builtin_bcdadd sign, neg of result is %d. Does not "
"match expected_result = %d\n",
result[0] & 0xF, BCD_NEG);
#else
abort();
#endif
/* Make a negative, b positive BCD numbers */
value_a = -1030507;
a = num2bcd(value_a, 0);
value_b = 1010101;
b = num2bcd(value_b, 0);
value_result = value_a - value_b;
exp_result = num2bcd(value_result, 0);
result = __builtin_bcdsub (a, b, 0);
for (i = 0; i < 16; i++)
if (exp_result[i] != result[i]) {
#if DEBUG
printf("ERROR: __builtin_bcdsub, neg result[%d] = %d does not match "
"expected_result[%d] = %d\n",
i, result[i], i, exp_result[i]);
#else
abort();
#endif
}
/* result should be positive, alt encoding */
if ((result[0] & 0xF) != BCD_NEG)
#if DEBUG
printf("ERROR: __builtin_bcdadd sign, of result is %d. Does not match "
"expected_result = %d\n",
result[0] & 0xF, BCD_NEG);
#else
abort();
#endif
/* Make a and b positive BCD numbers */
value_a = 1030507;
a = num2bcd(value_a, 1);
value_b = 1010101;
b = num2bcd(value_b, 1);
value_result = value_a - value_b;
exp_result = num2bcd(value_result, 1);
result = __builtin_bcdsub (a, b, 1);
for (i = 0; i < 16; i++)
if (exp_result[i] != result[i]) {
#if DEBUG
printf("ERROR:carll __builtin_bcdsub, pos result[%d] = %d does not "
"match expected_result[%d] = %d\n",
i, result[i], i, exp_result[i]);
#else
abort();
#endif
}
/* result should be positive */
if ((result[0] & 0xF) != BCD_POS1)
#if DEBUG
printf("ERROR: __builtin_bcdsub sign, result is %d. Does not match "
"expected_result = %d\n",
result[0] & 0xF, BCD_POS1);
#else
abort();
#endif
/* Test overflow add and subtract. */
a = maxbcd(BCD_POS0);
b = maxbcd(BCD_POS0);
if(__builtin_bcdadd_ofl (a, b, 0) == 0)
#if DEBUG
printf("ERROR: __builtin_bcdadd did not overflow as expected\n");
#else
abort();
#endif
value_a = 99999999;
a = num2bcd(value_a, 0);
value_b = 999999999;
b = num2bcd(value_b, 0);
if(__builtin_bcdadd_ofl (a, b, 0))
#if DEBUG
printf("ERROR: __builtin_bcdadd unexpectedly overflowed\n");
#else
abort();
#endif
a = maxbcd(BCD_NEG);
b = maxbcd(BCD_NEG);
if (__builtin_bcdsub_ofl (a, b, 0) == 0)
#if DEBUG
printf("ERROR: __builtin_bcdsub did not overflow as expected\n");
#else
abort();
#endif
value_a = -99999999;
a = num2bcd(value_a, 0);
value_b = -999999999;
b = num2bcd(value_b, 0);
if (__builtin_bcdsub_ofl (a, b, 0))
#if DEBUG
printf("ERROR: __builtin_bcdsub unexpectedly overflowed\n");
#else
abort();
#endif
/* Test arguments for valid/invalid */
if (__builtin_bcdinvalid (a))
#if DEBUG
printf("ERROR: __builtin_invalid input is unexpectedly invalid.\n");
#else
abort();
#endif
a[3] = 0xBB; /* an invalid BCD digit */
if (!__builtin_bcdinvalid (a))
#if DEBUG
printf("ERROR: __builtin_invalid input is unexpectedly valid.\n");
#else
abort();
#endif
value_a = 1020304;
a = num2bcd(value_a, 0);
value_b = 101010;
b = num2bcd(value_b, 0);
/* Test equality */
if (__builtin_bcdcmpeq (a, b))
#if DEBUG
printf("ERROR: __builtin__bcdcmpeq result is unexpectedly 1.\n");
#else
abort();
#endif
if (!__builtin_bcdcmpeq (a, a))
#if DEBUG
printf("ERROR: __builtin__bcdcmpeq result is unexpectedly 0.\n");
#else
abort();
#endif
/* Test a greater then b, inputs already setup this way. */
if (!__builtin_bcdcmpgt (a, b))
#if DEBUG
printf("ERROR: __builtin__bcdcmpgt result is unexpectedly 0.\n");
#else
abort();
#endif
if (__builtin_bcdcmpgt (b, a))
#if DEBUG
printf("ERROR: __builtin__bcdcmpgt result is unexpectedly 1.\n");
#else
abort();
#endif
if (__builtin_bcdcmpgt (a, a))
#if DEBUG
printf("ERROR: __builtin__bcdcmpgt input equal, result is unexpectedly "
"1.\n");
#else
abort();
#endif
if (!__builtin_bcdcmpge (a, b))
#if DEBUG
printf("ERROR: __builtin__bcdcmpge result is unexpectedly 0.\n");
#else
abort();
#endif
if (__builtin_bcdcmpge (b, a))
#if DEBUG
printf("ERROR: __builtin__bcdcmpge result is unexpectedly 1.\n");
#else
abort();
#endif
if (!__builtin_bcdcmpge (b, b))
#if DEBUG
printf("ERROR: __builtin__bcdcmpge inputs equal result is unexpectedly "
"0.\n");
#else
abort();
#endif
/* Test a less then b. */
value_a = 101010;
a = num2bcd(value_a, 0);
value_b = 1020304;
b = num2bcd(value_b, 0);
if (!__builtin_bcdcmplt (a, b))
#if DEBUG
printf("ERROR: __builtin__bcdcmplt result is unexpectedly 0.\n");
#else
abort();
#endif
if (__builtin_bcdcmplt (b, a))
#if DEBUG
printf("ERROR: __builtin__bcdcmplt result is unexpectedly 1.\n");
#else
abort();
#endif
if (__builtin_bcdcmplt (b, b))
#if DEBUG
printf("ERROR: __builtin__bcdcmplt inputs equal result is unexpectedly "
"1.\n");
#else
abort();
#endif
if (!__builtin_bcdcmple (a, b))
#if DEBUG
printf("ERROR: __builtin__bcdcmple result is unexpectedly 0.\n");
#else
abort();
#endif
if (__builtin_bcdcmple (b, a))
#if DEBUG
printf("ERROR: __builtin__bcdcmple result is unexpectedly 1.\n");
#else
abort();
#endif
if (!__builtin_bcdcmple (a, a))
#if DEBUG
printf("ERROR: __builtin__bcdcmple inputs equal result is unexpectedly "
"0.\n");
#else
abort();
#endif
/* Test multipy 10 */
value_a = 1020304;
a = num2bcd(value_a, 0);
value_result = value_a * 10;
exp_result = num2bcd(value_result, 0);
result = __builtin_bcdmul10 (a);
for (i = 0; i < 16; i++)
if (exp_result[i] != result[i]) {
#if DEBUG
printf("ERROR:carll __builtin_bcdmul10, pos result[%d] = %d does not "
"match expected_result[%d] = %d\n",
i, result[i], i, exp_result[i]);
#else
abort();
#endif
}
/* result should be positive */
if ((result[0] & 0xF) != BCD_POS0)
#if 0
printf("ERROR: __builtin_bcdmul10 sign, result is %d. Does not match "
"expected_result = %d\n",
result[0] & 0xF, BCD_POS1);
#else
abort();
#endif
/* Test divide 10 */
value_a = 1020304;
a = num2bcd(value_a, 0);
value_result = value_a / 10;
exp_result = num2bcd(value_result, 0);
result = __builtin_bcddiv10 (a);
for (i = 0; i < 16; i++)
if (exp_result[i] != result[i]) {
#if DEBUG
printf("ERROR:carll __builtin_bcddiv10, pos result[%d] = %d does not "
"match expected_result[%d] = %d\n",
i, result[i], i, exp_result[i]);
#else
abort();
#endif
}
/* result should be positive */
if ((result[0] & 0xF) != BCD_POS0)
#if DEBUG
printf("ERROR: __builtin_bcddiv10 sign, result is %d. Does not match "
"expected_result = %d\n",
result[0] & 0xF, BCD_POS1);
#else
abort();
#endif
value_a = 1020304;
exp_result_d128 = 1020304;
a = num2bcd(value_a, 0);
conv.ch = a;
conv.d128 = __builtin_bcd2dfp (a);
result_d128 = conv.d128;
if (result_d128 != exp_result_d128)
#if DEBUG
printf("ERROR: __builtin_bcd2dfp, result does not match expected_result."
"\n");
#else
abort();
#endif
return 0;
}
|