/ include / libm / typehelper.h
typehelper.h
 1  /*
 2   * Copyright (C) 2018-2020, Advanced Micro Devices, Inc. All rights reserved.
 3   *
 4   * Redistribution and use in source and binary forms, with or without modification,
 5   * are permitted provided that the following conditions are met:
 6   * 1. Redistributions of source code must retain the above copyright notice,
 7   *    this list of conditions and the following disclaimer.
 8   * 2. Redistributions in binary form must reproduce the above copyright notice,
 9   *    this list of conditions and the following disclaimer in the documentation
10   *    and/or other materials provided with the distribution.
11   * 3. Neither the name of the copyright holder nor the names of its contributors
12   *    may be used to endorse or promote products derived from this software without
13   *    specific prior written permission.
14   *
15   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18   * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
19   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
21   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   *
26   */
27  
28  #ifndef __LIBM_TYPEHELPER_H__
29  #define __LIBM_TYPEHELPER_H__
30  
31  #include <float.h>
32  #include <libm/types.h>
33  
34  static inline uint32_t
35  asuint32(float f)
36  {
37  	flt32u_t fl = {.f = f};
38  	return fl.i;
39  }
40  
41  static inline float
42  asfloat(uint32_t i)
43  {
44  	flt32u_t fl = {.i = i};
45  	return fl.f;
46  }
47  
48  static inline double
49  asdouble(uint64_t i)
50  {
51  	flt64_t dbl = {.i = i};
52  	return dbl.d;
53  }
54  
55  static inline uint64_t
56  asuint64(double f)
57  {
58  	flt64u_t fl = {.d = f};
59  	return fl.i;
60  }
61  
62  static inline double
63  eval_as_double(double d)
64  {
65      return d;
66  }
67  
68  static inline float 
69  eval_as_float(float f)
70  {
71      return f;
72  }
73  
74  static inline int32_t
75  cast_float_to_i32( float x )
76  {
77  	return (int32_t) x;
78  }
79  
80  static inline   int64_t
81  cast_double_to_i64( double x )
82  {
83  	return (int64_t) x;
84  }
85  
86  static inline float
87  cast_i32_to_float( int32_t x )
88  {
89  	return (float)x;
90  }
91  
92  static inline double
93  cast_i64_to_double( int64_t x )
94  {
95  	return (double)x;
96  }
97  
98  #endif	/* __LIBM_TYPEHELPER_H__ */