/ libft / src / ft_dprintf / src / dprintf_atoi64.c
dprintf_atoi64.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   dprintf_atoi64.c                                   :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: ll-hotel <ll-hotel@student.42lyon.fr>      +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2024/02/08 14:20:05 by ll-hotel          #+#    #+#             */
 9  /*   Updated: 2024/03/17 21:34:24 by ll-hotel         ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "dprintf_inner.h"
14  
15  void	ft_atoi64(char *out, int64_t n, const char *abase, uint8_t base)
16  {
17  	const uint16_t	log = ft_logb(n, base);
18  	const uint8_t	negative = n < 0;
19  	int64_t			tmp;
20  	uint16_t		i;
21  
22  	tmp = (n + (n == (int64_t)0x8000000000000000));
23  	tmp *= !negative - negative;
24  	i = log + negative;
25  	out[0] = '-';
26  	out[i] = abase[tmp % base];
27  	i += (n != 0);
28  	while (i > negative)
29  	{
30  		out[--i] = abase[tmp % base];
31  		tmp = tmp / base;
32  	}
33  	if ((uint64_t)n == 0x8000000000000000)
34  		out[10] += 1;
35  }
36  
37  void	ft_atoui64(char *out, uint64_t n, const char *abase, uint8_t base)
38  {
39  	uint16_t	i;
40  
41  	i = ft_logbu(n, base);
42  	out[i] = abase[n % base];
43  	i += (n != 0);
44  	while (i > 0)
45  	{
46  		out[--i] = abase[n % base];
47  		n /= base;
48  	}
49  }