File: printf_base.c

package info (click to toggle)
pocl 1.6-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 17,816 kB
  • sloc: lisp: 135,476; ansic: 64,403; cpp: 32,196; vhdl: 1,040; sh: 382; python: 336; makefile: 151; pascal: 140; java: 72; xml: 49
file content (920 lines) | stat: -rw-r--r-- 21,829 bytes parent folder | download | duplicates (4)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/* OpenCL built-in library: printf_base.c

   Copyright (c) 2018 Michal Babej / Tampere University of Technology

   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 "printf_base.h"

/* when BUILD_WITH_LIBC is enabled, uses C library's snprintf() for
 * float conversions. Otherwise uses builtin conversion (which currently
 * requires compiler-rt) */

#ifdef ENABLE_POCL_FLOAT_CONVERSION
#undef BUILD_WITH_LIBC
#else
#define BUILD_WITH_LIBC
#endif

/********************************************/

#ifdef BUILD_WITH_LIBC
int snprintf (char *str, size_t size, const char *format, ...);
#else

#include "errol/errol.h"

/* this is the function that generates a decimal representation string
 * of a float. May be any implementation as long as:
 *
 * 1) first returned digit in buffer is nonzero
 * 2) buffer is null-terminated
 * 3) return value is the decimal point position.
 *
 * e.g. 0.925 should return "925" in buffer and 0 as decimal point.
 *
 * Input is positive, non-zero, finite float. */
int
__pocl_generate_float_digits (FLOAT_T f, char *buffer)
{
  return errol4_dtoa (f, buffer);
}

#endif

void
__pocl_printf_putcf (param_t *p, char c)
{
  if (p->printf_buffer_index < p->printf_buffer_capacity)
    p->printf_buffer[p->printf_buffer_index++] = c;
}

void
__pocl_printf_puts (param_t *p, const char *string)
{
  char c;
  while ((c = *string++))
    {
      if (p->printf_buffer_index < p->printf_buffer_capacity)
        p->printf_buffer[p->printf_buffer_index++] = c;
    }
}

/* UNSIGNED long, base <= 10*/
void
__pocl_printf_ul_base (param_t *p, UINT_T num)
{
  char temp[SMALL_BUF_SIZE];
  unsigned i = 0, j = 0;
  unsigned digit;
  unsigned base = p->base;
  while (num > 0)
    {
      digit = num % base;
      num = num / base;
      temp[i++] = '0' + digit;
    }

  if (p->precision > 0)
    {
      for (; i < p->precision; i++)
        temp[i] = '0';
    }

  char *out = p->bf;
  for (j = i; j > 0; --j)
    *out++ = temp[j - 1];
  *out = 0;
}

/* UNSIGNED long, base == 16 */
void
__pocl_printf_ul16 (param_t *p, UINT_T num)
{
  char temp[SMALL_BUF_SIZE];
  unsigned i = 0, j = 0;
  unsigned digit;
  const unsigned base = 16;
  char digit_offset = (p->flags.uc ? 'A' : 'a');
  while (num > 0)
    {
      digit = num % base;
      num = num / base;
      temp[i++] = ((digit < 10) ? ('0' + digit) : (digit_offset + digit - 10));
    }

  if (p->precision > 0)
    {
      for (; i < p->precision; i++)
        temp[i] = '0';
    }

  char *out = p->bf;
  for (j = i; j > 0; --j)
    *out++ = temp[j - 1];
  *out = 0;
}

/* SIGNED long, base <= 10*/
void
__pocl_printf_l_base (param_t *p, INT_T num)
{
  UINT_T n;
  if (num < 0)
    {
      if (num == INT_T_MIN)
        n = (UINT_T)num;
      else
        n = -num;
      p->flags.sign = 1;
    }
  else
    {
      n = num;
      p->flags.sign = 0;
    }
  __pocl_printf_ul_base (p, n);
}

/* prints just the exponent */
void
__pocl_printf_exp (char *out, INT_T exp, unsigned min_output_chars)
{
  char temp[SMALL_BUF_SIZE];
  unsigned i = 0, j = 0;
  unsigned digit;

  if (exp < 0)
    {
      *out++ = '-';
      exp = -exp;
    }
  else
    *out++ = '+';

  do
    {
      digit = exp % 10;
      exp = exp / 10;
      temp[i++] = '0' + digit;
    }
  while (exp > 0);

  while (i < min_output_chars)
    {
      temp[i++] = '0';
    }

  for (j = i; j > 0; --j)
    *out++ = temp[j - 1];
  *out = 0;
}

/* print mantissa in nibbles (4bits) for %a */
void
__pocl_printf_nibbles (param_t *p, UINT_T num, INT_T exp,
                       unsigned max_fract_digits, int exact, int print_dec)
{
  char *out = p->bf;
  char temp[SMALL_BUF_SIZE];
  unsigned i = 0, available_digits = 0, written_digits = 0;
  unsigned digit, stop;
  const unsigned base = 16;
  char digit_offset = (p->flags.uc ? 'A' : 'a');
  unsigned trailing_zeroes = 0;
  int encountered_nonzero = 0;

  /* this loop will always produce MAX_NIBBLES+1 digits. */
  for (i = 0; i <= MAX_NIBBLES; ++i)
    {
      digit = num % base;
      num = num / base;
      temp[i] = ((digit < 10) ? ('0' + digit) : (digit_offset + digit - 10));
      /* count the trailing zeroes */
      if (digit)
        encountered_nonzero = 1;
      if (encountered_nonzero == 0)
        ++trailing_zeroes;
    }

  /* buffer now has "i" digits in reverse order */
  available_digits = i;

  /* always print first digit */
  *out++ = temp[--available_digits];

  /* precision == 0 */
  if (max_fract_digits == 0)
    goto SKIP_DECIMAL_PART;

  /* for exact printing, stop on trailing zeroes,
   * otherwise print max_digits digits. */
  if (exact)
    stop = trailing_zeroes;
  else
    stop = 0;

  /* decimal point if needed. */
  if (print_dec || (available_digits > stop))
    *out++ = '.';

  /* digits */
  while ((available_digits > stop) && (written_digits < max_fract_digits))
    {
      char c = temp[--available_digits];
      *out++ = c;
      written_digits++;
    }

SKIP_DECIMAL_PART:
  *out++ = (p->flags.uc ? 'P' : 'p');
  __pocl_printf_exp (out, exp, 0);
}

/*
 * style [−]0xh.hhhh p±d, where there is
 *
 * one hexadecimal digit
 * (which is nonzero if the argument is a normalized floating-point
 * number and is otherwise unspecified) before the decimal-point
 * character)
 *
 * and the number of hexadecimal digits after it is equal
 * to the precision;
 *
 * if the precision is missing, then the precision
 * is sufficient for an exact representation of the value;
 *
 * if the
 * precision is zero and the # flag is not specified, no decimal
 * point character appears.
 *
 * The letters abcdef are used for a conversion
 * and the letters ABCDEF for A conversion.
 *
 * The A conversion specifier
 * produces a number with X and P instead of x and p.
 *
 * The exponent
 * always contains at least one digit,
 * and only as many more digits as
 * necessary to represent the decimal exponent of 2.
 *
 * If the value is
 * zero, the exponent is zero.
 *
 * A double, halfn, floatn or doublen argument
 * representing an infinity or NaN is converted in the style of an f or F
 * conversion specifier.
 *
 * Binary implementations can choose the hexadecimal
 * digit to the left of the decimal-point character so that subsequent
 * digits align to nibble (4-bit) boundaries.
 *
 * For a and A conversions, the value is correctly rounded to a hexadecimal
 * floating number with the given precision.
 */
void
__pocl_printf_float_a (param_t *p, int print_dec, FLOAT_T f)
{
  union
  {
    FLOAT_T ff;
    FLOAT_UINT_T uu;
    FLOAT_INT_T ii;
  } tmp;

  tmp.ff = f;
  FLOAT_INT_T exp = L_EXPONENT (tmp.ii);
  FLOAT_UINT_T mant = L_MANTISSA (tmp.uu);

  /* handle denorms */
  if (exp == (-EXPBIAS) && mant > 0)
    {
      while (L_MSB_NIBBLE (mant) == 0)
        {
          exp -= 4;
          mant <<= 4;
        }
    }

  FLOAT_UINT_T max_fract_digits = 0;
  int exact = 0;

  if (p->precision < 0)
    {
      max_fract_digits = (FLOAT_UINT_T) (-1);
      exact = 1;
    }
  else
    max_fract_digits = (FLOAT_UINT_T)p->precision;

  if (tmp.uu == 0)
    exp = 0;
  else
    {
      mant |= LEADBIT;
      /* perform RTE rounding */
      if (max_fract_digits < MAX_NIBBLES)
        {
          /* No of bits that are beyond last wanted digit */
          FLOAT_UINT_T shift = EXPSHIFTBITS - (max_fract_digits * 4);
          FLOAT_UINT_T mask = (1UL << shift) - 1;
          FLOAT_UINT_T half = (1UL << (shift - 1));
          FLOAT_UINT_T rem = (mant & mask);
          FLOAT_UINT_T shifted_mant = mant >> shift;
          if ((rem == half) && ((shifted_mant & 1) == 0))
            mant = (shifted_mant) << shift;
          else if (rem >= half)
            mant = (shifted_mant + 1) << shift;
        }
    }

  __pocl_printf_nibbles (p, mant, exp, max_fract_digits, exact, print_dec);
  /* force putchw to print "0x" */
  p->flags.alt = 1;
  p->base = 16;
  p->flags.nonzeroparam = 1;
}

#ifdef BUILD_WITH_LIBC
void
__pocl_printf_float_libc (param_t *p, FLOAT_T f)
{
  char outfmt[128];
  char conv = p->conv;
  if (p->flags.uc)
    conv -= 32;
  int prec = p->precision;
  const char str[] = "%%%s%s%s%s%s%.0d%s%.0d" "%c";
  snprintf(outfmt, sizeof outfmt,
           str,
           p->flags.align_left ? "-" : "",
           p->flags.always_sign ? "+" : "",
           p->flags.space ? " " : "",
           p->flags.alt ? "#" : "",
           p->flags.zero ? "0" : "",
           p->width,
           prec != -1 ? "." : "",
           prec != -1 ? prec : 0,
           conv
           );

  snprintf (p->bf, BUFSIZE, outfmt, f);
  p->bf[BUFSIZE - 1] = 0;
  __pocl_printf_puts (p, p->bf);
}

#else

/* Rounds a decimal float to 'prec' decimal places, using RTE.
 * Note that input buf must have '0' as its first char
 * (to make round up easier).
 *
 * in F mode, round to prec decimal places from dec_point;
 * in E mode, round to 2+prec decimal places (one for the extra 0 we added, one
 * for the always present first digit) */
void
__pocl_printf_float_round (FLOAT_T f, char *buf, int dec_point, int prec,
                           int e_mode)
{
  /* invalid args, no rounding */
  if (prec < 0 || prec > 18)
    return;

  int round_point = prec;
  if (e_mode)
    round_point += 2;
  else
    round_point += dec_point;

  /* result rounds to zero */
  if (round_point < 0)
    {
      buf[0] = 0;
      return;
    }

  char *p = buf;
  while (*p)
    ++p;
  int len = p - buf;
  /* if round_point >= len, we're done */
  if (round_point >= len)
    return;

  /* round_point < len; find out if the digits
   * after round_point are exactly halfway between */
  int i = round_point;
  int direction = (buf[i] - '5');
  ++i;
  int is_half = (direction == 0);
  if (is_half && i < len)
    {
      while (i < len && (buf[i] == '0'))
        ++i;
      is_half = (i == len);
      direction = (i != len);
    }

  i = round_point - 1;

  if (is_half) /* RTE */
    {
      char last_dig = buf[i];
      if ((last_dig - '0') % 2)
        direction = 1; /* odd round up */
    }
  if (direction > 0) /* round up */
    {
      int carry = 0;
      do
        {
          char c = buf[i];
          carry = (c == '9');
          if (carry)
            buf[i] = '0';
          else
            buf[i] += 1;
          --i;
        }
      while (carry);
    }

  buf[round_point] = 0;
}

/* if needed, drops the extra zero added for rounding */
char *
__pocl_printf_float_round_buf (FLOAT_T f, char *buf, int *exp, int prec,
                               int e_mode)
{
  __pocl_printf_float_round (f, buf, *exp, prec, e_mode);

  if (buf[0] == '0' && buf[1])
    {
      ++buf;
      *exp -= 1;
    }

  return buf;
}

/* print the decimal float in buf with 'e' format. float must be already
 * rounded */
void
__pocl_printf_float_e (param_t *p, char *buf, int point, int print_dec,
                       int notrailing0)
{
  char *out = p->bf;
  char *in = buf;

  int prec = p->precision;

  int exp = point;
  int printed_dec = 0;

  /* we're always printing one digit for integral part */
  --exp;

  char c;
  /* always print first digit */
  *out++ = *in++;

  if (print_dec || (prec > 0))
    *out++ = '.';

  /* decimal part */
  if (prec > 0)
    {
      c = *in++;
      while (printed_dec < prec)
        {
          /* note: for notrailing0 mode, we cannot simply not print '0' here,
           * since the buffer may contain trailing 0 as result of rounding;
           * we must remove trailing zeroes manually. */
          if (c)
            {
              *out++ = c;
              c = *in++;
            }
          else
            *out++ = '0';

          ++printed_dec;
        }
    }

  /* remove trailing zeroes & dec. point if needed */
  if (notrailing0)
    {
      char *tmp = out - 1;
      while (tmp > p->bf && (*tmp == '0'))
        --tmp;
      if (*tmp == '.')
        --tmp;
      out = tmp + 1;
    }

  *out++ = (p->flags.uc ? 'E' : 'e');
  __pocl_printf_exp (out, exp, 2);
}

/* print the decimal float in buf with 'f' format. float must be already
 * rounded */
void
__pocl_printf_float_f (param_t *p, char *buf, int point, int print_dec,
                       int notrailing0)
{
  int prec = p->precision;
  int i;
  char *out = p->bf; // output
  char *in = buf;    // input
  char c;
  /* pointer to where '.' was printed, for trailing 0 removal */
  char *decpoint_ptr = NULL;

  /* bytes written for mantissa. must be <= prec */
  int written = 0;

  /* if required, print leading zeros */
  if (point <= 0)
    {
      int poi = point;
      *out++ = '0';
      if (print_dec || (prec > 0))
        {
          decpoint_ptr = out;
          *out++ = '.';
        }
      while ((poi < 0) && (written < prec))
        {
          *out++ = '0';
          ++poi;
          ++written;
        }
      c = *in++;
    }
  /* else print integral part */
  else
    {
      c = *in++;
      for (i = 0; i < point; ++i)
        {
          if (c)
            {
              *out++ = c;
              c = *in++;
            }
          else
            *out++ = '0';
        }
      if (print_dec || (prec > 0))
        {
          decpoint_ptr = out;
          *out++ = '.';
        }
    }

  /* print decimal part */
  while (written < prec)
    {
      if (c)
        {
          *out++ = c;
          c = *in++;
        }
      else
        *out++ = '0';

      ++written;
    }

  *out = 0;

  /* remove trailing zeroes if needed*/
  if (notrailing0 && (decpoint_ptr != NULL))
    {
      out -= 1;
      unsigned i = 0;
      while (out > decpoint_ptr && *out == '0')
        {
          *out-- = 0;
        }
      if (*out == '.')
        *out = 0;
    }
}

#endif

/* prints the number (float or integer) in p->bf,
 * taking care of various flags. */
void
__pocl_printf_putchw (param_t *p)
{
  char ch;
  int n = p->width;
  /* For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to it.
   */
  int althex = (p->flags.nonzeroparam && p->flags.alt && p->base == 16);
  /* For o conversion, it increases the precision, if and only if necessary,
   * to force the first digit of the result to be a zero */
  int altoct = (p->bf[0] != '0' && p->flags.alt && p->base == 8);
  int sign_required = (p->flags.always_sign || p->flags.sign);
  int space_required = (p->flags.space && p->flags.sign == 0);
  char *bf = p->bf;

  /* Number of filling characters */
  while (*bf++ && n > 0)
    n--;
  if (sign_required)
    n--;
  if (space_required)
    n--;
  if (althex)
    n -= 2;
  if (altoct)
    n--;

  /* Fill with space to align to the right, before alternate or sign */
  if (!p->flags.zero && !p->flags.align_left)
    {
      while (n-- > 0)
        __pocl_printf_putcf (p, ' ');
    }

  if (space_required)
    __pocl_printf_putcf (p, ' ');

  /* print sign */
  if (sign_required)
    {
      __pocl_printf_putcf (p, (p->flags.sign ? '-' : '+'));
    }

  /* Alternate */
  if (althex)
    {
      __pocl_printf_putcf (p, '0');
      __pocl_printf_putcf (p, (p->flags.uc ? 'X' : 'x'));
    }
  else if (altoct)
    {
      __pocl_printf_putcf (p, '0');
    }

  /* Fill with zeros, after alternate or sign */
  if (p->flags.zero)
    {
      while (n-- > 0)
        __pocl_printf_putcf (p, '0');
    }

  /* Put actual buffer */
  __pocl_printf_puts (p, p->bf);

  /* Fill with space to align to the left, after string */
  if (!p->flags.zero && p->flags.align_left)
    {
      while (n-- > 0)
        __pocl_printf_putcf (p, ' ');
    }
}

void
__pocl_printf_puts_ljust (param_t *p, const char *string, size_t width,
                          ssize_t max_width)
{
  char c;
  size_t written = 0;
  if (max_width < 0)
    max_width = (__SIZE_MAX__ >> 1);
  while ((c = *string++))
    {
      if (written < max_width)
        __pocl_printf_putcf (p, c);
      ++written;
    }
  while (written < width)
    {
      if (written < max_width)
        __pocl_printf_putcf (p, ' ');
      ++written;
    }
}

void
__pocl_printf_puts_rjust (param_t *p, const char *string, size_t width,
                          ssize_t max_width)
{
  char c;
  size_t i, strleng = 0, written = 0;
  if (max_width < 0)
    max_width = (__SIZE_MAX__ >> 1);

  const char *tmp = string;
  while ((c = *tmp++))
    ++strleng;

  for (i = strleng; i < width; ++i)
    {
      if (written < max_width)
        __pocl_printf_putcf (p, ' ');
      ++written;
    }

  while ((c = *string++))
    {
      if (written < max_width)
        __pocl_printf_putcf (p, c);
      ++written;
    }
}

void
__pocl_printf_ptr (param_t *p, const void *ptr)
{
  p->base = 16;
  p->flags.uc = 0;
  p->flags.alt = 1;
  p->flags.sign = 0;
  p->flags.nonzeroparam = 1;
  __pocl_printf_ul16 (p, (uintptr_t)ptr);
  __pocl_printf_putchw (p);
}

/* prints NANs and INFs */
void
__pocl_printf_nonfinite (param_t *p, const char *ptr)
{
  char c;
  char *dest = p->bf;
  while ((c = *ptr++))
    *dest++ = c;
  *dest = 0;
  /* When applied to infinite and NaN values, the -, +, and space flag
   * characters have their usual meaning; the # and 0 flag characters
   * have no effect */
  p->flags.zero = 0;

  __pocl_printf_putchw (p);
}

void
__pocl_printf_ulong (param_t *p, UINT_T u)
{
  if (p->base == 16)
    {
      p->flags.nonzeroparam = (u > 0 ? 1 : 0);
      __pocl_printf_ul16 (p, u);
    }
  else
    __pocl_printf_ul_base (p, u);

  __pocl_printf_putchw (p);
}

void
__pocl_printf_long (param_t *p, INT_T i)
{
  __pocl_printf_l_base (p, i);
  __pocl_printf_putchw (p);
}

void
__pocl_printf_float (param_t *p, FLOAT_T f)
{
#ifdef BUILD_WITH_LIBC
  __pocl_printf_float_libc (p, f);
#else

  int print_dec = 0;
  if (p->precision == 0)
    {
      if (p->flags.alt)
        {
          /* For a, A, e, E, f, F, g, and G conversions, the result of
           * converting a floating-point number always contains a decimal-point
           * character, even if no digits follow it. */
          print_dec = 1;
        }
    }

  /* f is not flipped in caller, because libc mode */
  p->flags.sign = SIGNBIT(f) ? 1 : 0;

  if (p->flags.sign)
    f = -f;

  if (p->conv == 'a')
    {
      __pocl_printf_float_a (p, print_dec, f);
      __pocl_printf_putchw (p);
      return;
    }

  char float_digits[BUFSIZE];
  char *rounded_digits = float_digits;
  int P, X, notrail0;
  int round_exp = 0, dec_point = 0;

  /* save is required for 'g' mode with float vectors */
  int saved_prec = p->precision;
  if (p->precision < 0)
    p->precision = 6;

  /* prepend a zero digit to the buffer, makes rounding up easier */
  int nonzero = (f != 0.0f);
  float_digits[0] = '0';
  float_digits[1] = 0;
  if (nonzero)
    dec_point = __pocl_generate_float_digits (f, float_digits + 1);
  round_exp = dec_point + 1;

  switch (p->conv)
    {
    case 'e':
      if (nonzero)
        rounded_digits = __pocl_printf_float_round_buf (
            f, float_digits, &round_exp, p->precision, 1);
      __pocl_printf_float_e (p, rounded_digits, round_exp, print_dec, 0);
      break;
    case 'f':
      if (nonzero)
        rounded_digits = __pocl_printf_float_round_buf (
            f, float_digits, &round_exp, p->precision, 0);
      __pocl_printf_float_f (p, rounded_digits, round_exp, print_dec, 0);
      break;

    /* Let P equal the precision if nonzero,
     * 6 if the precision is omitted,
     * or 1 if the precision is zero.
     *
     * Then, if a conversion with style E
     * would have an exponent of X:
     *
     * — if P > X ≥ −4, the conversion is with
     * style f (or F) and precision P − (X + 1).
     *
     * — otherwise, the conversion is with
     * style e (or E) and precision P − 1.
     *
     * Finally, unless the # flag
     * is used, any trailing zeros are removed from the fractional portion of
     * the result and the decimal-point character is removed if there is no
     * fractional portion remaining. */

    /* TODO: dec_point is exponent BEFORE rounding, so decision between 'e' and
     * 'f' happens before rounding. is this correct behaviour ? */
    case 'g':

      notrail0 = (p->flags.alt ? 0 : 1);

      P = p->precision;
      if (P == 0)
        P = 1;

      X = dec_point - 1;
      if ((P > X) && (X >= -4))
        {
          p->precision = P - (X + 1);
          if (nonzero)
            rounded_digits = __pocl_printf_float_round_buf (
                f, float_digits, &round_exp, p->precision, 0);
          __pocl_printf_float_f (p, rounded_digits, round_exp, print_dec,
                                 notrail0);
        }
      else
        {
          p->precision = P - 1;
          if (nonzero)
            rounded_digits = __pocl_printf_float_round_buf (
                f, float_digits, &round_exp, p->precision, 1);
          __pocl_printf_float_e (p, rounded_digits, round_exp, print_dec,
                                 notrail0);
        }
      break;
    }

  p->precision = saved_prec;
  __pocl_printf_putchw (p);
#endif
}