Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cinttypes>
- #include <cstdlib>
- #include <cstring>
- #include <ctime>
- #include <iostream>
- std::uint32_t tbl[0x1000000];
- const char b64alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- void base64b(const char source[], std::size_t size) {
- std::cout << "Run bitwise \n";
- auto result = new char[size*4/3];
- struct timespec start;
- clock_gettime(CLOCK_MONOTONIC, &start);
- for (auto counter = 0; counter < 100; ++counter) {
- for (auto i = 0, j = 0; i < size; i +=3, j += 4) {
- auto block = (std::uint8_t(source[i]) << 16) |
- (std::uint8_t(source[i+1]) << 8) |
- std::uint8_t(source[i+2]);
- result[j+0] = b64alpha[(block>>18)&0x3f];
- result[j+1] = b64alpha[(block>>12)&0x3f];
- result[j+2] = b64alpha[(block>>6)&0x3f];
- result[j+3] = b64alpha[block&0x3f];
- }
- asm volatile("" :: "m"(result));
- }
- struct timespec finish;
- clock_gettime(CLOCK_MONOTONIC, &finish);
- auto delta = std::int64_t(finish.tv_nsec) + std::int64_t(finish.tv_sec) * 1000000000 -
- std::int64_t(start.tv_nsec) - std::int64_t(start.tv_sec) * 1000000000;
- std::cout << (double(delta) / 1000000000) << "s \n";
- (std::cout << "Base64: ").write(result, 40) << "\n";
- delete[] result;
- }
- void base64t(const char source[], std::size_t size) {
- std::cout << "Run tabular \n";
- auto result = new char[size*4/3];
- struct timespec start;
- clock_gettime(CLOCK_MONOTONIC, &start);
- for (auto counter = 0; counter < 100; ++counter) {
- for (auto i = 0, j = 0; i < size; i +=3, j += 4) {
- uint32_t block;
- std::memcpy(&block, &source[i], sizeof(block));
- block &= 0xffffff;
- std::memcpy(&result[j], &tbl[block], sizeof(uint32_t));
- }
- asm volatile("" :: "m"(result));
- }
- struct timespec finish;
- clock_gettime(CLOCK_MONOTONIC, &finish);
- auto delta = std::int64_t(finish.tv_nsec) + std::int64_t(finish.tv_sec) * 1000000000 -
- std::int64_t(start.tv_nsec) - std::int64_t(start.tv_sec) * 1000000000;
- std::cout << (double(delta) / 1000000000) << "s \n";
- (std::cout << "Base64: ").write(result, 40) << "\n";
- delete[] result;
- }
- char source[3*10*1024*1024];
- int main() {
- std::cout << "Prepare table\n";
- for (auto i = 0; i < 0x1000000; i++) {
- auto block = ((i & 0xff) << 16) | (i & 0xff00) | (i >> 16);
- tbl[i] = std::uint32_t(b64alpha[(block>>18)&0x3f]) |
- std::uint32_t(b64alpha[(block>>12)&0x3f])<<8 |
- std::uint32_t(b64alpha[(block>>6)&0x3f])<<16 |
- std::uint32_t(b64alpha[block&0x3f])<<24;
- }
- const char alpha[] = "abcdefghijklmnopqrstuvwxyz0123456789 {}\":";
- for (auto i = 0; i < sizeof(source); ++i) {
- source[i] = alpha[rand() % (sizeof(alpha) - 1)];
- }
- (std::cout << "Text source data:").write(source, 30) << "\n";
- base64b(source, sizeof(source));
- base64t(source, sizeof(source));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement