<commit_before>/**
 * @file IOStream_dtoa.cpp
 * @version 1.0
 *
 * @section License
 * Copyright (C) 2011, <NAME>
 *
 * A high performance implementation of standard C functions for
 * conversion from signed and unsigned numbers to character string.
 *
 * @section References
 * 1. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=627899
 */

#include "Cosa/IOStream.hh"

#if !defined(COSA_IOSTREAM_STDLIB)
#include <avr/pgmspace.h>

static const unsigned long digits8[] PROGMEM = {
  1073741824,
  134217728,
  16777216,
  2097152,
  262144,
  32768,
  4096,
  512,
  64,
  8,
  1,
};

static const unsigned long digits10[] PROGMEM = {
  1000000000,
  100000000,
  10000000,
  1000000,
  100000,
  10000,
  1000,
  100,
  10,
  1,
};

static const unsigned long digits16[] PROGMEM = {
  268435456,
  16777216,
  1048576,
  65536,
  4096,
  256,
  16,
  1,
};

static const unsigned long digits32[] PROGMEM = {
  1073741824,
  33554432,
  1048576,
  32768,
  1024,
  32,
  1,
};

static const unsigned long digits36[] PROGMEM = {
  2176782336,
  60466176,
  1679616,
  46656,
  1296,
  36,
  1,
};

static const char letters[] PROGMEM = "0123456789abcdefghijklmnopqrstuvwxyz";

char*
IOStream::ultoa(unsigned long __val, char *__s, int base)
{
  unsigned char max;
  const unsigned long *d;
  unsigned char i, j, k, first = 1;

  switch(base) {
  case 8:
    max = sizeof(digits8) / sizeof(unsigned long);
    d = digits8;
    break;

  case 16:
    max = sizeof(digits16) / sizeof(unsigned long);
    d = digits16;
    break;

  case 32:
    max = sizeof(digits32) / sizeof(unsigned long);
    d = digits32;
    break;

  case 36:
    max = sizeof(digits36) / sizeof(unsigned long);
    d = digits36;
    break;

  default:
    max = sizeof(digits10) / sizeof(unsigned long);
    d = digits10;
    break;
  }

  for (i = j = 0; i < max; i++) {
    unsigned long check = pgm_read_dword(d + i);
    if (check > __val) {
      if (first)
	continue;
      __s[j++] = '0';
      continue;
    }
    first = k = 0;
    while(check <= __val) {
      __val -= check;
      k++;
    }

    __s[j++] = pgm_read_byte(letters + k);
  }

  if (first)
    __s[j++] = '0';
  __s[j] = 0;

  return (__s);
}

char*
IOStream::ltoa(long __val, char *__s, int base)
{
  if (__val >= 0)
    return (ultoa((unsigned long)__val, __s, base));
  __s[0] = '-';
  return (ultoa((unsigned long)(-__val), __s + 1, base) - 1);
}
#endif
<commit_msg>Improving integer to string conversion performance.<commit_after>/**
 * @file IOStream_dtoa.cpp
 * @version 1.0
 *
 * @section License
 * Copyright (C) 2011, <NAME>
 *
 * A high performance implementation of standard C functions for
 * conversion from signed and unsigned numbers to character string.
 *
 * @section References
 * 1. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=627899
 */

#include "Cosa/IOStream.hh"

#if !defined(COSA_IOSTREAM_STDLIB_DTOA)
#include <avr/pgmspace.h>

static const unsigned long digits8[] PROGMEM = {
  1073741824,
  134217728,
  16777216,
  2097152,
  262144,
  32768,
  4096,
  512,
  64,
  8,
  1,
};

static const unsigned long digits10[] PROGMEM = {
  1000000000,
  100000000,
  10000000,
  1000000,
  100000,
  10000,
  1000,
  100,
  10,
  1,
};

static const char letters[] PROGMEM = "0123456789abcdef";

char*
IOStream::ultoa(unsigned long __val, char *__s, int base)
{
  unsigned char i, j, k, l, first;
  const uint8_t* p;
  first = 1;
  j = 0;

  if (__val != 0UL) {
    if (base == 2) {
      // Optimize for base(2)
      first = 0;
      p = ((uint8_t*) &__val) + sizeof(__val) - 1;
      for (i = 0; i < 4 && *p == 0; i++, p--);
      k = *p--;
      for (l = 0; l < 8; l++) {
	if (k & 0x80) break;
	k <<= 1;
      }
      for (; i < 4; i++, l = 0, k = *p--) {
	for (; l < 8; l++) {
	  __s[j++] = (k & 0x80) ? '1' : '0';
	  k <<= 1;
	}
      }
    }
    else if (base == 16) {
      // Optimize for base(16)
      first = 0;
      p = ((uint8_t*) &__val) + sizeof(__val) - 1;
      for (i = 0; i < 4 && *p == 0; i++, p--);
      k = *p--;
      if (k & 0xf0) __s[j++] = pgm_read_byte(letters + (k >> 4));
      __s[j++] = pgm_read_byte(letters + (k & 0xf));
      for (i++; i < 4; i++) {
	k = *p--;
	__s[j++] = pgm_read_byte(letters + (k >> 4));
	__s[j++] = pgm_read_byte(letters + (k & 0xf));
      }
    }
    else {
      const unsigned long *d;
      unsigned char max;
      if (base == 8) {
	max = sizeof(digits8) / sizeof(unsigned long);
	d = digits8;
      }
      else {
	max = sizeof(digits10) / sizeof(unsigned long);
	d = digits10;
      }
      for (i = 0; i < max; i++) {
	unsigned long check = pgm_read_dword(d + i);
	if (check > __val) {
	  if (first)
	    continue;
	  __s[j++] = '0';
	  continue;
	}
	first = k = 0;
	while(check <= __val) {
	  __val -= check;
	  k++;
	}
	__s[j++] = pgm_read_byte(letters + k);
      }
    }
  }

  if (first)
    __s[j++] = '0';
  __s[j] = 0;

  return (__s);
}

char*
IOStream::ltoa(long __val, char *__s, int base)
{
  if (__val >= 0 || base != 10)
    return (ultoa((unsigned long)__val, __s, base));
  __s[0] = '-';
  return (ultoa((unsigned long)(-__val), __s + 1, base) - 1);
}

char*
IOStream::utoa(unsigned int __val, char *__s, int base)
{
  return (ultoa(((unsigned long) __val) & 0xffff, __s, base));
}

char*
IOStream::itoa(int __val, char *__s, int base)
{
  return (ltoa((long)__val, __s, base));
}

#endif

{'fraction_non_alphanumeric': 0.16320807627593942, 'fraction_numerical': 0.09179285847822023, 'mean_word_length': 2.641933287950987, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4871181', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2163, 'n_words': 578}