-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmegaphone.cpp
71 lines (64 loc) · 2.21 KB
/
megaphone.cpp
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* megaphone.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ltouzali <ltouzali@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 17:02:24 by ltouzali #+# #+# */
/* Updated: 2024/07/08 20:26:32 by babonnet ### ########.fr */
/* */
/* ************************************************************************** */
#include <string>
#include <iostream>
#if defined(__AVX512__) || defined(__AVX2__)
# include "megaphone.hpp"
# include <immintrin.h>
//
__attribute__((__always_inline__, __nodebug__))
inline void to_upper_avx(std::string& str) {
__attribute__((aligned(SIZE))) int len = str.length();
const Aligned lower_a = Setreg('a');
const Aligned lower_z = Setreg('z');
const Aligned upper_mask = Setreg(0xDF); // 0xDF -> 11011111
Prefetch(str.data());
Prefetch(str.data() + SIZE);
PROCESS_CHARS_AVX(str, len, lower_z, lower_a, upper_mask);
for (int i = 0; i < len; ++i) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] &= 0xDF; // STR AND= 11011111
}
}
}
#endif
int megaphone(char **av)
{
av++;
if (!*av)
{
std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << std::endl;
return (1);
}
for (; *av; av++)
{
std::string str = *av;
#if defined(__AVX2__) || defined(__AVX512__)
to_upper_avx(str);
#else
for (int i = 0; i < str.length(); ++i) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] &= 0xDF; // STR AND= 11011111
}
}
#endif
std::cout << str;
if (*(av + 1))
std::cout << " ";
}
std::cout << std::endl;
return (0);
}
int main(__attribute__((unused)) int ac, char **av)
{
megaphone(av);
}