ReactOS 0.4.16-dev-853-g88d9285
corecrt_internal_fltintrn.h
Go to the documentation of this file.
1//
2// corecrt_internal_fltintrn.h
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Floating point conversion routines for internal use. This is a C++ header.
7//
8#pragma once
9
10#include <corecrt_internal.h>
11#include <float.h>
12#include <stdint.h>
13#include <stdlib.h>
14
15
16
17//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18//
19// Types
20//
21//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22template <typename FloatingType>
24
25template <>
27{
29 {
30 mantissa_bits = FLT_MANT_DIG,
31 exponent_bits = sizeof(float) * CHAR_BIT - FLT_MANT_DIG,
32
33 maximum_binary_exponent = FLT_MAX_EXP - 1,
34 minimum_binary_exponent = FLT_MIN_EXP - 1,
35
36 exponent_bias = 127
37 };
38
40 {
41 exponent_mask = (1u << (exponent_bits )) - 1,
42 normal_mantissa_mask = (1u << (mantissa_bits )) - 1,
43 denormal_mantissa_mask = (1u << (mantissa_bits - 1)) - 1,
44
45 special_nan_mantissa_mask = (1u << (mantissa_bits - 2))
46 };
47
49 {
50 uint32_t _mantissa : mantissa_bits - 1;
51 uint32_t _exponent : exponent_bits;
53 };
54
55 static_assert(sizeof(components_type) == sizeof(float), "unexpected components size");
56};
57
58template <>
60{
62 {
63 mantissa_bits = DBL_MANT_DIG,
64 exponent_bits = sizeof(double) * CHAR_BIT - DBL_MANT_DIG,
65
66 maximum_binary_exponent = DBL_MAX_EXP - 1,
67 minimum_binary_exponent = DBL_MIN_EXP - 1,
68
69 exponent_bias = 1023
70 };
71
73 {
74 exponent_mask = (1ull << (exponent_bits )) - 1,
75 normal_mantissa_mask = (1ull << (mantissa_bits )) - 1,
76 denormal_mantissa_mask = (1ull << (mantissa_bits - 1)) - 1,
77
78 special_nan_mantissa_mask = (1ull << (mantissa_bits - 2))
79 };
80
82 {
83 uint64_t _mantissa : mantissa_bits - 1;
84 uint64_t _exponent : exponent_bits;
86 };
87
88 static_assert(sizeof(components_type) == sizeof(double), "unexpected components size");
89};
90
92{
93 finite,
98};
99
101{
102 trailing,
104};
105
106// Precision is the number of digits after the decimal point, but
107// this has different implications for how many digits need to be
108// generated based upon how the number will be formatted.
110{
111 fixed, // 123.456 %f style requires '3' precision to generate 6 digits to format "123.456".
112 scientific // 123.456 %e style requires '5' precision to generate 6 digits to format "1.23456e+02".
113};
114
115// This rounding mode is used to know if we are using functions like gcvt vs printf
117{
118 legacy,
120};
121
122inline __acrt_fp_class __cdecl __acrt_fp_classify(double const& value) throw()
123{
124 using floating_traits = __acrt_floating_type_traits<double>;
125 using components_type = floating_traits::components_type;
126
127 components_type const& components = reinterpret_cast<components_type const&>(value);
128
129 bool const value_is_nan_or_infinity = components._exponent == (1u << floating_traits::exponent_bits) - 1;
130 if (!value_is_nan_or_infinity)
131 {
133 }
134 else if (components._mantissa == 0)
135 {
137 }
138 else if (components._sign == 1 && components._mantissa == floating_traits::special_nan_mantissa_mask)
139 {
141 }
142 else if (components._mantissa & floating_traits::special_nan_mantissa_mask) // Quiet NAN
143 {
145 }
146 else // Signaling NAN
147 {
149 }
150}
151
152inline bool __cdecl __acrt_fp_is_negative(double const& value) throw()
153{
154 using floating_traits = __acrt_floating_type_traits<double>;
155 using components_type = floating_traits::components_type;
156
157 components_type const& components = reinterpret_cast<components_type const&>(value);
158
159 return components._sign == 1;
160}
161
163{
164 int sign; // Zero if positive otherwise negative
165 int decpt; // Exponent of floating point number
166 char* mantissa; // Pointer to mantissa in string form
167};
168
170
171//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
172//
173// Floating Point Conversion Routines
174//
175//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
177
178// Result buffer count for __acrt_fp_format has a minimum value that depends on the precision requested.
179// This requirement originates and propagates from the fp_format_e_internal function (in convert\cvt.cpp)
180// This macro can be used to annotate result_buffer_count in the below functions
181#define _In_fits_precision_(precision_arg) \
182 _When_(precision_arg <= 0, _Pre_satisfies_(_Curr_ > 9)) \
183 _When_(precision_arg > 0, _Pre_satisfies_(_Curr_ > 9 + precision_arg))
184
185_Success_(return == 0)
187 _In_ double const* value,
190 _Out_writes_(scratch_buffer_count) char* scratch_buffer,
196 _Inout_ __crt_cached_ptd_host& ptd
197 );
198
203 _In_ size_t buffer_count,
204 _In_ int digits,
206 _In_ __acrt_has_trailing_digits trailing_digits,
208 _Inout_ __crt_cached_ptd_host& ptd
209 );
210
213 _In_ unsigned precision,
214 _In_ __acrt_precision_style precision_style,
217 _In_ size_t buffer_count
218 );
219
#define __cdecl
Definition: accygwin.h:79
#define _Maybe_unsafe_(buffer_annotation, expr)
_In_ size_t scratch_buffer_count
bool __cdecl __acrt_fp_is_negative(double const &value)
_strflt * STRFLT
_In_ size_t _In_ int _In_ int _In_ uint64_t _In_ __acrt_rounding_mode _Inout_ __crt_cached_ptd_host & ptd
__acrt_has_trailing_digits
__acrt_has_trailing_digits __cdecl __acrt_fltout(_In_ _CRT_DOUBLE value, _In_ unsigned precision, _In_ __acrt_precision_style precision_style, _Out_ STRFLT result, _Out_writes_z_(buffer_count) char *buffer, _In_ size_t buffer_count)
#define _In_fits_precision_(precision_arg)
__acrt_fp_class __cdecl __acrt_fp_classify(double const &value)
errno_t __cdecl __acrt_fp_strflt_to_string(_Out_writes_z_(buffer_count) char *buffer, _When_((digits > 0), _In_ _Pre_satisfies_(buffer_count > digits+1)) _When_((digits<=0), _In_ _Pre_satisfies_(buffer_count > 1)) _In_ size_t buffer_count, _In_ int digits, _Inout_ STRFLT value, _In_ __acrt_has_trailing_digits trailing_digits, _In_ __acrt_rounding_mode rounding_mode, _Inout_ __crt_cached_ptd_host &ptd)
_In_ size_t _In_ int _In_ int _In_ uint64_t _In_ __acrt_rounding_mode rounding_mode
floating_traits::components_type components_type
Definition: cvt.cpp:357
errno_t __cdecl __acrt_fp_format(double const *const value, char *const result_buffer, size_t const result_buffer_count, char *const scratch_buffer, size_t const scratch_buffer_count, int const format, int const precision, uint64_t const options, __acrt_rounding_mode rounding_mode, __crt_cached_ptd_host &ptd)
Definition: cvt.cpp:786
result_buffer_count char *const result_buffer
Definition: cvt.cpp:111
INT32 int32_t
Definition: types.h:71
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
#define CHAR_BIT
Definition: urlcache.c:62
GLuint buffer
Definition: glext.h:5915
GLenum GLenum GLuint components
Definition: glext.h:9620
GLuint64EXT * result
Definition: glext.h:11304
GLenum GLint GLint * precision
Definition: glext.h:7539
#define DBL_MIN_EXP
Definition: float.h:81
#define FLT_MANT_DIG
Definition: float.h:90
#define DBL_MAX_EXP
Definition: float.h:78
#define FLT_MIN_EXP
Definition: float.h:96
#define DBL_MANT_DIG
Definition: float.h:75
#define FLT_MAX_EXP
Definition: float.h:93
static const int digits[]
Definition: decode.c:71
static const char mbstate_t *static wchar_t const char mbstate_t *static const wchar_t int *static double
Definition: string.c:89
static float(__cdecl *square_half_float)(float x
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define _Inout_
Definition: no_sal2.h:162
#define _Success_(c)
Definition: no_sal2.h:84
#define _Pre_satisfies_(e)
Definition: no_sal2.h:64
#define _Inout_updates_z_(s)
Definition: no_sal2.h:186
#define _Out_writes_(s)
Definition: no_sal2.h:176
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define _When_(c, a)
Definition: no_sal2.h:38
#define exponent_mask
Definition: format.c:58
size_t const result_buffer_count
Definition: tmpfile.cpp:196
int errno_t
Definition: corecrt.h:615
Definition: pdh_main.c:96
#define _CRT_END_C_HEADER
Definition: vcruntime.h:42
#define _CRT_BEGIN_C_HEADER
Definition: vcruntime.h:40
size_t const buffer_count
Definition: xtoa.cpp:36
#define const
Definition: zconf.h:233