File: common.h

package info (click to toggle)
picolibc 1.8.11-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 50,064 kB
  • sloc: ansic: 404,031; asm: 24,984; sh: 2,585; python: 2,289; perl: 680; pascal: 329; exp: 287; makefile: 222; cpp: 71; xml: 40
file content (105 lines) | stat: -rw-r--r-- 2,458 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2018 Ulf Adams
//
// The contents of this file may be used under the terms of the Apache License,
// Version 2.0.
//
//    (See accompanying file LICENSE-Apache or copy at
//     http://www.apache.org/licenses/LICENSE-2.0)
//
// Alternatively, the contents of this file may be used under the terms of
// the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE-Boost or copy at
//     https://www.boost.org/LICENSE_1_0.txt)
//
// Unless required by applicable law or agreed to in writing, this software
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
#ifndef RYU_COMMON_H
#define RYU_COMMON_H

#include <stdint.h>
#include <string.h>
#include <stdbool.h>

#define assert(x)
// #include <assert.h>

#if __SIZEOF_POINTER__ == 4
#define RYU_32_BIT_PLATFORM
#endif

#ifdef __SIZEOF_INT128__
#define HAS_UINT128
#endif

#if __SIZEOF_DOUBLE__ == 8
#define RYU64 double
#elif __SIZEOF_LONG_DOUBLE__ == 8
#define RYU64 long double
#endif

static inline int
max_int(int a, int b)
{
    return a > b ? a : b;
}
static inline int
min_int(int a, int b)
{
    return a < b ? a : b;
}

// Returns e == 0 ? 1 : [log_2(5^e)]; requires 0 <= e <= 3528.
int32_t __log2pow5(const int32_t e);

#define log2pow5(e) __log2pow5(e)

// Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
int32_t __ceil_log2pow5(const int32_t e);

#define ceil_log2pow5(e) __ceil_log2pow5(e)

// Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528.
int32_t __pow5bits(const int32_t e);

#define pow5bits(e) __pow5bits(e)

// Returns floor(log_10(2^e)); requires 0 <= e <= 1650.
uint32_t __log10Pow2(const int32_t e);

#define log10Pow2(e) __log10Pow2(e)

// Returns floor(log_10(5^e)); requires 0 <= e <= 2620.
uint32_t __log10Pow5(const int32_t e);

#define log10Pow5(e) __log10Pow5(e)

static inline uint32_t
float_to_bits(const float f)
{
    uint32_t bits = 0;
    memcpy(&bits, &f, sizeof(float));
    return bits;
}

#ifdef RYU64

static inline uint64_t
ryu64_to_bits(const RYU64 d)
{
    uint64_t bits = 0;
    memcpy(&bits, &d, sizeof(d));
    return bits;
}

#endif

// These tables are generated by PrintDoubleLookupTable.
#define DOUBLE_POW5_INV_BITCOUNT 125
#define DOUBLE_POW5_BITCOUNT     125

void __double_computePow5(const uint32_t i, uint64_t * const result);

void __double_computeInvPow5(const uint32_t i, uint64_t * const result);

#endif // RYU_COMMON_H