Skip to content

Commit

Permalink
[added] RL78/G16_FPB related files for Arduino library.
Browse files Browse the repository at this point in the history
  • Loading branch information
MikiyaKobayashi committed Jun 26, 2023
1 parent ee86b80 commit 131c17d
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 0 deletions.
142 changes: 142 additions & 0 deletions cores/rl78g16/dtostrf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//#include "api/deprecated-avr-comp/avr/dtostrf.c.impl"

#if 1
#include <stdio.h>
#include <string.h>
#include <math.h>

static size_t printNumber(unsigned long n, uint8_t base, char *inputbuff)
{
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];

*str = '\0';

// prevent crash if called with base == 1
if (base < 2)
{
base = 10;
}

do
{
char c = (char)(n % base);
n /= base;

*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);

strcpy(inputbuff,str);

return strlen(str);
}


char *dtostrf (double val, signed char width, unsigned char prec, char *sout)
{
char fmt[20];
double number = val;
unsigned char digits = prec;
signed short n;
signed short width_local = width;
memset(fmt, 0, sizeof(fmt));

n = 0;

if (isnan(number))
{
strcpy(fmt, "nan");
strcpy(sout, fmt);
return sout;
}
if (isinf(number))
{
strcpy(fmt, "inf");
strcpy(sout, fmt);
return sout;
}
if (number > 4294967040.0)
{
strcpy(fmt, "ovf");
strcpy(sout, fmt);
return sout;
}
if (number <-4294967040.0)
{
strcpy(fmt, "ovf");
strcpy(sout, fmt);
return sout;
}

// Handle negative numbers
if (number < 0.0)
{
// n += print('-');
fmt[n] = '-';
n++;
number = -number;
}


// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;

number += rounding;

// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
// n += print(int_part);
n+= printNumber(int_part, 10, &fmt[n]);


// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
fmt[n] = '.';
n++;
// n += print(".");
}

// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
unsigned int toPrint = (unsigned int)remainder;
// n += print(toPrint);
n+= printNumber((unsigned long)toPrint, 10, &fmt[n]);
remainder -= toPrint;
}

if(width_local < 0)
{
/* left */
width_local = -width_local;
if(n <= width_local)
{
n = width_local;
}
strncpy((char *)sout, (const char *)fmt, (size_t)n);
*(sout+n) = '\0';
}
else
{
/* right */
signed short offset = 0;
memset((void *)sout, ' ', (size_t)width_local);
if(n <= width_local)
{
offset = width_local - n;
strncpy((char *)sout+offset, (const char *)fmt, (size_t)n);
*(sout+n+offset) = '\0';
}
else
{
strncpy((char *)sout, (const char *)fmt, (size_t)n);
*(sout+n) = '\0';
}
}
return sout;
}
#endif
125 changes: 125 additions & 0 deletions cores/rl78g16/itoa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright (c) 2014 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

char* ltoa( long value, char *string, int radix )
{
char tmp[33];
char *tp = tmp;
long i;
unsigned long v;
int sign;
char *sp;

if ( string == NULL )
{
return 0 ;
}

if (radix > 36 || radix <= 1)
{
return 0 ;
}

sign = (radix == 10 && value < 0);
if (sign)
{
v = -value;
}
else
{
v = (unsigned long)value;
}

while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+'0';
else
*tp++ = i + 'a' - 10;
}

sp = string;

if (sign)
*sp++ = '-';
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;

return string;
}

char* ultoa( unsigned long value, char *string, int radix )
{
char tmp[33];
char *tp = tmp;
long i;
unsigned long v = value;
char *sp;

if ( string == NULL )
{
return 0;
}

if (radix > 36 || radix <= 1)
{
return 0;
}

while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+'0';
else
*tp++ = i + 'a' - 10;
}

sp = string;


while (tp > tmp)
*sp++ = *--tp;
*sp = 0;

return string;
}

char* itoa( int value, char *string, int radix )
{
return ltoa( value, string, radix ) ;
}

char* utoa( unsigned int value, char *string, int radix )
{
return ultoa( value, string, radix ) ;
}

#ifdef __cplusplus
} // extern "C"
#endif

0 comments on commit 131c17d

Please sign in to comment.