13cab2bb3Spatrick //=== lib/fp_trunc.h - high precision -> low precision conversion *- C -*-===// 23cab2bb3Spatrick // 33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information. 53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 63cab2bb3Spatrick // 73cab2bb3Spatrick //===----------------------------------------------------------------------===// 83cab2bb3Spatrick // 93cab2bb3Spatrick // Set source and destination precision setting 103cab2bb3Spatrick // 113cab2bb3Spatrick //===----------------------------------------------------------------------===// 123cab2bb3Spatrick 133cab2bb3Spatrick #ifndef FP_TRUNC_HEADER 143cab2bb3Spatrick #define FP_TRUNC_HEADER 153cab2bb3Spatrick 163cab2bb3Spatrick #include "int_lib.h" 173cab2bb3Spatrick 183cab2bb3Spatrick #if defined SRC_SINGLE 193cab2bb3Spatrick typedef float src_t; 203cab2bb3Spatrick typedef uint32_t src_rep_t; 213cab2bb3Spatrick #define SRC_REP_C UINT32_C 223cab2bb3Spatrick static const int srcSigBits = 23; 233cab2bb3Spatrick 243cab2bb3Spatrick #elif defined SRC_DOUBLE 253cab2bb3Spatrick typedef double src_t; 263cab2bb3Spatrick typedef uint64_t src_rep_t; 273cab2bb3Spatrick #define SRC_REP_C UINT64_C 283cab2bb3Spatrick static const int srcSigBits = 52; 293cab2bb3Spatrick 303cab2bb3Spatrick #elif defined SRC_QUAD 313cab2bb3Spatrick typedef long double src_t; 323cab2bb3Spatrick typedef __uint128_t src_rep_t; 333cab2bb3Spatrick #define SRC_REP_C (__uint128_t) 343cab2bb3Spatrick static const int srcSigBits = 112; 353cab2bb3Spatrick 363cab2bb3Spatrick #else 373cab2bb3Spatrick #error Source should be double precision or quad precision! 383cab2bb3Spatrick #endif // end source precision 393cab2bb3Spatrick 403cab2bb3Spatrick #if defined DST_DOUBLE 413cab2bb3Spatrick typedef double dst_t; 423cab2bb3Spatrick typedef uint64_t dst_rep_t; 433cab2bb3Spatrick #define DST_REP_C UINT64_C 443cab2bb3Spatrick static const int dstSigBits = 52; 453cab2bb3Spatrick 463cab2bb3Spatrick #elif defined DST_SINGLE 473cab2bb3Spatrick typedef float dst_t; 483cab2bb3Spatrick typedef uint32_t dst_rep_t; 493cab2bb3Spatrick #define DST_REP_C UINT32_C 503cab2bb3Spatrick static const int dstSigBits = 23; 513cab2bb3Spatrick 523cab2bb3Spatrick #elif defined DST_HALF 53d89ec533Spatrick #ifdef COMPILER_RT_HAS_FLOAT16 54d89ec533Spatrick typedef _Float16 dst_t; 55d89ec533Spatrick #else 563cab2bb3Spatrick typedef uint16_t dst_t; 57d89ec533Spatrick #endif 583cab2bb3Spatrick typedef uint16_t dst_rep_t; 593cab2bb3Spatrick #define DST_REP_C UINT16_C 603cab2bb3Spatrick static const int dstSigBits = 10; 613cab2bb3Spatrick 62*810390e3Srobert #elif defined DST_BFLOAT 63*810390e3Srobert typedef __bf16 dst_t; 64*810390e3Srobert typedef uint16_t dst_rep_t; 65*810390e3Srobert #define DST_REP_C UINT16_C 66*810390e3Srobert static const int dstSigBits = 7; 67*810390e3Srobert 683cab2bb3Spatrick #else 693cab2bb3Spatrick #error Destination should be single precision or double precision! 703cab2bb3Spatrick #endif // end destination precision 713cab2bb3Spatrick 723cab2bb3Spatrick // End of specialization parameters. Two helper routines for conversion to and 733cab2bb3Spatrick // from the representation of floating-point data as integer values follow. 743cab2bb3Spatrick srcToRep(src_t x)753cab2bb3Spatrickstatic __inline src_rep_t srcToRep(src_t x) { 763cab2bb3Spatrick const union { 773cab2bb3Spatrick src_t f; 783cab2bb3Spatrick src_rep_t i; 793cab2bb3Spatrick } rep = {.f = x}; 803cab2bb3Spatrick return rep.i; 813cab2bb3Spatrick } 823cab2bb3Spatrick dstFromRep(dst_rep_t x)833cab2bb3Spatrickstatic __inline dst_t dstFromRep(dst_rep_t x) { 843cab2bb3Spatrick const union { 853cab2bb3Spatrick dst_t f; 863cab2bb3Spatrick dst_rep_t i; 873cab2bb3Spatrick } rep = {.i = x}; 883cab2bb3Spatrick return rep.f; 893cab2bb3Spatrick } 903cab2bb3Spatrick 913cab2bb3Spatrick #endif // FP_TRUNC_HEADER 92