|
| 1 | +/* Copyright 2016 Samsung Electronics Co., Ltd. |
| 2 | + * Copyright 2016 University of Szeged. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Unit test for fdlibm |
| 19 | + */ |
| 20 | + |
| 21 | +#include "test-common.h" |
| 22 | + |
| 23 | +static bool passed = true; |
| 24 | + |
| 25 | +static void |
| 26 | +check_int (const char *expr, int computed, int expected) |
| 27 | +{ |
| 28 | + printf ("%s = %d [expected=%d] ", expr, computed, expected); |
| 29 | + |
| 30 | + bool result = computed == expected; |
| 31 | + printf ("%s\n", result ? "PASS" : "FAIL"); |
| 32 | + |
| 33 | + passed &= result; |
| 34 | +} /* check_int */ |
| 35 | + |
| 36 | +static void |
| 37 | +check_double (const char *expr, double computed, double expected) |
| 38 | +{ |
| 39 | + uint64_t *computed_bits_p = (uint64_t *) &computed; |
| 40 | + uint64_t *expected_bits_p = (uint64_t *) &expected; |
| 41 | + |
| 42 | + printf ("%s = 0x%lx [expected=0x%lx] ", expr, *computed_bits_p, *expected_bits_p); |
| 43 | + |
| 44 | + bool result; |
| 45 | + if (isnan (computed) && isnan (expected)) |
| 46 | + { |
| 47 | + result = true; |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + int64_t diff = (int64_t) (*computed_bits_p - *expected_bits_p); |
| 52 | + if (diff < 0) |
| 53 | + { |
| 54 | + diff = -diff; |
| 55 | + } |
| 56 | + |
| 57 | + if (diff <= 1) /* tolerate 1 bit of differene in the last place */ |
| 58 | + { |
| 59 | + result = true; |
| 60 | + if (diff != 0) |
| 61 | + { |
| 62 | + printf ("APPROX "); |
| 63 | + } |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + result = false; |
| 68 | + } |
| 69 | + } |
| 70 | + printf ("%s\n", result ? "PASS" : "FAIL"); |
| 71 | + |
| 72 | + passed &= result; |
| 73 | +} /* check_double */ |
| 74 | + |
| 75 | +int |
| 76 | +main (int __attr_unused___ argc, |
| 77 | + char __attr_unused___ **argv) |
| 78 | +{ |
| 79 | +#define INF INFINITY |
| 80 | +#include "test-fdlibm.inc.h" |
| 81 | + |
| 82 | + return passed ? 0 : 1; |
| 83 | +} /* main */ |
0 commit comments