Joerg Sonnenberger | b8e9b02 | 2014-05-29 00:49:57 +0000 | [diff] [blame] | 1 | //=== lib/fp_trunc.h - high precision -> low precision conversion *- C -*-===// |
| 2 | // |
Chandler Carruth | 7a739a0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Joerg Sonnenberger | b8e9b02 | 2014-05-29 00:49:57 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Set source and destination precision setting |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef FP_TRUNC_HEADER |
| 14 | #define FP_TRUNC_HEADER |
| 15 | |
| 16 | #include "int_lib.h" |
| 17 | |
Ahmed Bougacha | d982553 | 2015-05-12 18:33:42 +0000 | [diff] [blame] | 18 | #if defined SRC_SINGLE |
| 19 | typedef float src_t; |
| 20 | typedef uint32_t src_rep_t; |
| 21 | #define SRC_REP_C UINT32_C |
| 22 | static const int srcSigBits = 23; |
| 23 | |
| 24 | #elif defined SRC_DOUBLE |
Joerg Sonnenberger | b8e9b02 | 2014-05-29 00:49:57 +0000 | [diff] [blame] | 25 | typedef double src_t; |
| 26 | typedef uint64_t src_rep_t; |
| 27 | #define SRC_REP_C UINT64_C |
| 28 | static const int srcSigBits = 52; |
| 29 | |
| 30 | #elif defined SRC_QUAD |
| 31 | typedef long double src_t; |
| 32 | typedef __uint128_t src_rep_t; |
| 33 | #define SRC_REP_C (__uint128_t) |
| 34 | static const int srcSigBits = 112; |
| 35 | |
| 36 | #else |
| 37 | #error Source should be double precision or quad precision! |
| 38 | #endif //end source precision |
| 39 | |
| 40 | #if defined DST_DOUBLE |
| 41 | typedef double dst_t; |
| 42 | typedef uint64_t dst_rep_t; |
| 43 | #define DST_REP_C UINT64_C |
| 44 | static const int dstSigBits = 52; |
| 45 | |
| 46 | #elif defined DST_SINGLE |
| 47 | typedef float dst_t; |
| 48 | typedef uint32_t dst_rep_t; |
| 49 | #define DST_REP_C UINT32_C |
| 50 | static const int dstSigBits = 23; |
| 51 | |
Ahmed Bougacha | d982553 | 2015-05-12 18:33:42 +0000 | [diff] [blame] | 52 | #elif defined DST_HALF |
| 53 | typedef uint16_t dst_t; |
| 54 | typedef uint16_t dst_rep_t; |
| 55 | #define DST_REP_C UINT16_C |
| 56 | static const int dstSigBits = 10; |
| 57 | |
Joerg Sonnenberger | b8e9b02 | 2014-05-29 00:49:57 +0000 | [diff] [blame] | 58 | #else |
| 59 | #error Destination should be single precision or double precision! |
| 60 | #endif //end destination precision |
| 61 | |
| 62 | // End of specialization parameters. Two helper routines for conversion to and |
| 63 | // from the representation of floating-point data as integer values follow. |
| 64 | |
Saleem Abdulrasool | 85a0d71 | 2015-10-10 21:21:28 +0000 | [diff] [blame] | 65 | static __inline src_rep_t srcToRep(src_t x) { |
Joerg Sonnenberger | b8e9b02 | 2014-05-29 00:49:57 +0000 | [diff] [blame] | 66 | const union { src_t f; src_rep_t i; } rep = {.f = x}; |
| 67 | return rep.i; |
| 68 | } |
| 69 | |
Saleem Abdulrasool | 85a0d71 | 2015-10-10 21:21:28 +0000 | [diff] [blame] | 70 | static __inline dst_t dstFromRep(dst_rep_t x) { |
Joerg Sonnenberger | b8e9b02 | 2014-05-29 00:49:57 +0000 | [diff] [blame] | 71 | const union { dst_t f; dst_rep_t i; } rep = {.i = x}; |
| 72 | return rep.f; |
| 73 | } |
| 74 | |
| 75 | #endif // FP_TRUNC_HEADER |