File: printf_base.h

package info (click to toggle)
pocl 6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 25,304 kB
  • sloc: lisp: 149,513; ansic: 103,778; cpp: 54,947; python: 1,513; sh: 949; ruby: 255; pascal: 226; tcl: 180; makefile: 173; java: 72; xml: 49
file content (158 lines) | stat: -rw-r--r-- 4,937 bytes parent folder | download | duplicates (2)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* OpenCL built-in library: printf_base.h

   Copyright (c) 2018 Michal Babej / Tampere University

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

#include "pocl_types.h"

/* printing largest double with %f formatter requires about ~1024 digits for
 * integral part, plus precision digits for decimal part, plus some space for
 * modifiers. */
#define BUFSIZE 1200
#define SMALL_BUF_SIZE 64

#define NULL ((void*)0)

/* Produce a SPIR-V compliant bitcode where the format string is
   in the constant address space (2 in SPIR-V). Address space cast
   in Workgroup.cc in case of native compilation. */
#define PRINTF_FMT_STR_AS __attribute__ ((address_space (2)))

#ifdef PRINTF_BUFFER_AS_ID
#define PRINTF_BUFFER_AS __attribute__ ((address_space (PRINTF_BUFFER_AS_ID)))
#else
#define PRINTF_BUFFER_AS
#endif

typedef intptr_t ssize_t;

#ifdef cl_khr_int64

#define INT_T int64_t
#define UINT_T uint64_t
#define INT_T_MIN LONG_MIN
#define INT_T_MAX LONG_MAX

#else

#define INT_T int32_t
#define UINT_T uint32_t
#define INT_T_MIN INT_MIN
#define INT_T_MAX INT_MAX

#endif


#ifdef cl_khr_fp64
#define FLOAT_T double
#define FLOAT_INT_T long
#define FLOAT_UINT_T ulong

#define NAN __builtin_nan ("1")
#define INFINITY (__builtin_inf())
#define SIGNBIT __builtin_signbit

#define EXPBITS      0x7ff0000000000000L
#define EXPBIAS      1023
#define EXPSHIFTBITS 52
#define MANTBITS     0x000fffffffffffffUL
#define LEADBIT      0x0010000000000000UL
#define MSB_NIBBLE   0x000f000000000000UL
#define MAX_NIBBLES  13

#define L_EXPONENT(x) (((x & EXPBITS) >> EXPSHIFTBITS) - EXPBIAS)
#define L_MANTISSA(x) (x & MANTBITS)
#define L_SIGNBIT(x) (x >> 63)
#define L_MSB_NIBBLE(x) (x & MSB_NIBBLE)

#else

#define FLOAT_T float
#define FLOAT_INT_T int
#define FLOAT_UINT_T uint

#define NAN __builtin_nanf ("1")
#define INFINITY (__builtin_inff())
#define SIGNBIT __builtin_signbitf

#define EXPBITS      0x7f800000
#define EXPBIAS      127
#define EXPSHIFTBITS 23
#define MANTBITS     0x007fffff
#define LEADBIT      0x00800000
#define MSB_NIBBLE   0x00780000
#define MAX_NIBBLES  6

#define L_EXPONENT(x) (((x & EXPBITS) >> EXPSHIFTBITS) - EXPBIAS)
#define L_MANTISSA(x) (x & MANTBITS)
#define L_SIGNBIT(x) (x >> 31)
#define L_MSB_NIBBLE(x) (x & MSB_NIBBLE)

#endif

/* Conversion flags */
typedef struct
{
  unsigned char zero : 1;        /**  Leading zeros */
  unsigned char alt : 1;         /**  alternate form */
  unsigned char align_left : 1;  /**  0 == align right (default), 1 == align left */
  unsigned char uc : 1;          /**  Upper case (for base16 only) */
  unsigned char always_sign : 1; /**  plus flag (always display sign) */
  unsigned char sign : 1;        /**   the actual sign **/
  unsigned char space : 1;       /** If the first character of a signed conversion is not
                          a sign, print a space */
  unsigned char nonzeroparam : 1; /** number to print is not zero */
} flags_t;

typedef struct
{
  char *bf;             /**  Buffer to output */
  PRINTF_BUFFER_AS char *restrict printf_buffer;
  uint printf_buffer_index;
  uint printf_buffer_capacity;
  int precision;       /**  field precision */
  unsigned width;      /**  field width */
  unsigned base;
  flags_t flags;
  char conv;
} param_t;

void __pocl_printf_putchw (param_t *p);

void __pocl_printf_putcf (param_t *p, char c);

void __pocl_printf_puts (param_t *p, const char *string);

void __pocl_printf_nonfinite (param_t *p, const char *ptr);

void __pocl_printf_puts_ljust (param_t *p, const char *string, size_t width,
                               ssize_t max_width);

void __pocl_printf_puts_rjust (param_t *p, const char *string, size_t width,
                               ssize_t max_width);

void __pocl_printf_ptr (param_t *p, const void *ptr);

void __pocl_printf_ulong (param_t *p, UINT_T u);

void __pocl_printf_long (param_t *p, INT_T i);

void __pocl_printf_float (param_t *p, FLOAT_T f);