-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_types_0.c
166 lines (157 loc) · 3.44 KB
/
utils_types_0.c
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
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
/**
* u_long_to_str - Converts an unsigned long to its string representation
* @num: The unsigned long integer
*
* Return: The string representation of the unsigned long integer
*/
char *u_long_to_str(unsigned long num)
{
char *str;
unsigned long rem = num;
char size = 20, j;
str = malloc(sizeof(char) * (size + 1));
if (str)
{
*(str + size) = '\0';
mem_set(str, size, '0');
j = size - 1;
while (rem > 0)
{
*(str + j) = (rem % 10) + '0';
rem /= 10;
j--;
}
str = trim_start(str, '0', TRUE);
str = num == 0 ? str_cat("0", "", FALSE) : str;
}
return (str);
}
/**
* long_to_str - Converts a long to its string representation
* @num: The long integer
*
* Return: The string representation of the long integer
*/
char *long_to_str(long num)
{
char *str;
long rem = num;
char sign = rem < 0 ? -1 : 1;
char size = 20, j;
str = malloc(sizeof(char) * (size + 1));
if (str)
{
*(str + size) = '\0';
mem_set(str, size, '0');
j = size - 1;
if (rem == 0)
*(str + j) = '0';
while ((sign == -1 && rem < 0) || (sign == 1 && rem > 0))
{
*(str + j) = (rem % 10) * sign + '0';
rem /= 10;
j--;
}
if (sign < 0)
*(str + j) = '-';
str = trim_start(str, '0', TRUE);
str = num == 0 ? str_cat("0", "", FALSE) : str;
}
return (str);
}
/**
* ptr_to_str - Converts a pointer to its string representation
* @ptr: The pointer
*
* Return: The string representation of the pointer
*/
char *ptr_to_str(void *ptr)
{
int i, size;
uintptr_t tmp;
char *str, *str0;
if (ptr)
{
tmp = (uintptr_t)ptr;
size = sizeof(ptr) * 2;
str = malloc(sizeof(char) * (size + 1));
if (str)
{
str0 = malloc(sizeof(char) * (2 + 1));
if (str0)
{
mem_set(str, size, '0');
for (i = 0; i < size; i++)
{
*(str + i) = (tmp % 16) < 10 ? (tmp % 16) + '0'
: (tmp % 16) - 10 + 'a';
tmp /= 16;
}
*(str + i) = '\0';
*(str0 + 0) = '0';
*(str0 + 1) = 'x';
*(str0 + 2) = '\0';
rev_string(str);
str = trim_start(str, '0', TRUE);
str = *str == '\0' ? str_cat("0", "", FALSE) : str;
str = str_cat(str0, str, TRUE);
}
if (!str0)
free(str);
}
}
else
{
str = str_copy("(nil)");
}
return (str);
}
/**
* is_invalid - Checks if a given float is invalid
* @flt_info: The float info struct to check
*
* Return: NULL if it is valid, otherwise inf or nan
*/
char *is_invalid(float_info_t *flt_info)
{
uchar_t exp_bits_on = 0, exp_all_on;
short mant_bits_on = 0;
char lsb_on = FALSE, msb_on = FALSE;
int i;
if (flt_info != NULL)
{
for (i = 0; *(flt_info->exponent + i) != '\0'; i++)
{
if (*(flt_info->exponent + i) == '1')
exp_bits_on++;
}
exp_all_on = i == exp_bits_on;
for (i = 0; *(flt_info->mantissa + i) != '\0'; i++)
{
msb_on = i == 0 && *(flt_info->mantissa + i) == '1' ? TRUE : msb_on;
lsb_on = *(flt_info->mantissa + i + 1) == '\0'
&& *(flt_info->mantissa + i) == '1' ? TRUE : lsb_on;
if (*(flt_info->mantissa + i) == '1')
mant_bits_on++;
}
if (exp_all_on)
{
if ((flt_info->sign == '0' && mant_bits_on == 0)
|| (flt_info->sign == '1' && mant_bits_on == 0))
{
return (str_copy("inf"));
}
else if ((flt_info->sign == '0' && mant_bits_on == 1 && lsb_on)
|| (flt_info->sign == '0' && mant_bits_on == 2 && lsb_on && msb_on)
|| (flt_info->sign == '0' && mant_bits_on == i))
{
return (str_copy("nan"));
}
}
}
return (NULL);
}