Advertisement
magneto903

wasm_left_code

Mar 23rd, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. int edges_start[3000];
  5. int edges_end[3000];
  6. float edges_len[3000];
  7.  
  8. int g_links_id[1000][100];
  9. float g_links_weight[1000][100];
  10.  
  11. int cache_id[1000];
  12. float cache_distance[1000];
  13. int cache_prev[1000];
  14. int cache_links_id[1000][100];
  15. float cache_links_weight[1000][100];
  16.  
  17. int queue_id[1000];
  18. float queue_distance[1000];
  19. int queue_prev[1000];
  20. int queue_links_id[1000][100];
  21. float queue_links_weight[1000][100];
  22.  
  23. int queue_links_id_len[1000];
  24.  
  25. // MUSL memset implementation:
  26. // https://github.com/esmil/musl/blob/master/src/string/memset.c
  27.  
  28. #include <stdint.h>
  29. #include <string.h>
  30.  
  31. void* __attribute__((weak)) memset(void* dest, int c, size_t n)
  32. {
  33.     unsigned char* s = dest;
  34.     size_t k;
  35.  
  36.     if(!n)
  37.     {
  38.         return dest;
  39.     }
  40.  
  41.     s[0] = s[n - 1] = (unsigned char)c;
  42.  
  43.     if(n <= 2)
  44.     {
  45.         return dest;
  46.     }
  47.  
  48.     s[1] = s[n - 2] = (unsigned char)c;
  49.     s[2] = s[n - 3] = (unsigned char)c;
  50.  
  51.     if(n <= 6)
  52.     {
  53.         return dest;
  54.     }
  55.  
  56.     s[3] = s[n - 4] = (unsigned char)c;
  57.  
  58.     if(n <= 8)
  59.     {
  60.         return dest;
  61.     }
  62.  
  63.  
  64.     k = -(uintptr_t)s & 3;
  65.     s += k;
  66.     n -= k;
  67.     n &= (unsigned long)-4;
  68.     n /= 4;
  69.  
  70.     // Cast to void first to prevent alignment warning
  71.     uint32_t* ws = (uint32_t*)(void*)s;
  72.     uint32_t wc = c & 0xFF;
  73.     wc |= ((wc << 8) | (wc << 16) | (wc << 24));
  74.  
  75.     /* Pure C fallback with no aliasing violations. */
  76.     for(; n; n--, ws++)
  77.     {
  78.         *ws = wc;
  79.     }
  80.  
  81.     return dest;
  82. }
  83.  
  84.  
  85. #include <string.h>
  86.  
  87. typedef int word;
  88.  
  89. #define wsize sizeof(word)
  90. #define wmask (wsize - 1)
  91.  
  92. void* memcpy(void* dst0, const void* src0, size_t length)
  93. {
  94.     char* dst = dst0;
  95.     const char* src = src0;
  96.     size_t t;
  97.  
  98.     if(length == 0 || dst == src)
  99.     {
  100.         /* nothing to do */
  101.         goto done;
  102.     }
  103.  
  104. #define TLOOP(s) if (t) TLOOP1(s)
  105. #define TLOOP1(s) do { s; } while (--t)
  106.  
  107.  
  108.     if((uintptr_t)dst < (uintptr_t)src)
  109.     {
  110.  
  111.         t = (uintptr_t)src; /* only need low bits */
  112.         if((t | (uintptr_t)dst) & wmask)
  113.         {
  114.            
  115.             if((t ^ (uintptr_t)dst) & wmask || length < wsize)
  116.             {
  117.                 t = length;
  118.             }
  119.             else
  120.             {
  121.                 t = wsize - (t & wmask);
  122.             }
  123.             length -= t;
  124.             TLOOP1(*dst++ = *src++);
  125.         }
  126.    
  127.         t = length / wsize;
  128.         // Silence warning for alignment change by casting to void*
  129.         TLOOP(*(word*)(void*)dst = *(const word*)(const void*)src; src += wsize; dst += wsize);
  130.         t = length & wmask;
  131.         TLOOP(*dst++ = *src++);
  132.     }
  133.     else
  134.     {
  135.    
  136.         src += length;
  137.         dst += length;
  138.         t = (uintptr_t)src;
  139.  
  140.         if((t | (uintptr_t)dst) & wmask)
  141.         {
  142.             if((t ^ (uintptr_t)dst) & wmask || length <= wsize)
  143.             {
  144.                 t = length;
  145.             }
  146.             else
  147.             {
  148.                 t &= wmask;
  149.             }
  150.  
  151.             length -= t;
  152.             TLOOP1(*--dst = *--src);
  153.         }
  154.  
  155.         t = length / wsize;
  156.         // Silence warning for alignment change by casting to void*
  157.         TLOOP(src -= wsize; dst -= wsize; *(word*)(void*)dst = *(const word*)(const void*)src);
  158.         t = length & wmask;
  159.         TLOOP(*--dst = *--src);
  160.     }
  161. done:
  162.     return (dst0);
  163. }
  164.  
  165.  
  166. void init_cpp(int L) {
  167.   for (int i=0; i < L; i++) {
  168.     queue_links_id_len[i] = 0;
  169.     cache_id[i] = -2;
  170.     queue_id[i] = -2;
  171.     cache_distance[i] =  100000;
  172.     queue_distance[i] = 100000;
  173.     cache_prev[i] = -2;
  174.     queue_prev[i] = -2;
  175.     for (int j=0; j < 100; j++) {
  176.       queue_links_id[i][j] = -2;
  177.       queue_links_weight[i][j] = 100000;
  178.       cache_links_id[i][j] = -2;
  179.       cache_links_weight[i][j] = 100000;
  180.     }
  181.   }
  182. }
  183.  
  184. void init_edges_cpp() {
  185.   for (int i=0; i < 3000; i++) {
  186.     edges_start[i] = -2;
  187.     edges_end[i] = -2;
  188.     edges_len[i] = -2.0;
  189.   }
  190.  
  191.   for (int i=0; i < 1000; i++) {
  192.     for (int j=0; j < 100; j++) {
  193.       g_links_id[i][j] = -2;
  194.       g_links_weight[i][j] = -2.0;
  195.     }
  196.   }
  197. }
  198.  
  199. void add_edge_cpp(int index, int start, int end, float len) {
  200.   edges_start[index] = start;
  201.   edges_end[index] = end;
  202.   edges_len[index] = len;
  203. }
  204.  
  205. void fill_graph_cpp() {
  206.   for (int i=0; i < 3000; i++) {
  207.     int s = edges_start[i];
  208.     int e = edges_end[i];
  209.     float len = edges_len[i];
  210.  
  211.     if (s == -2) {
  212.       break;
  213.     }
  214.  
  215.     int links_len = 0;
  216.  
  217.     for (int j=0; j < 100; j++) {
  218.       if (g_links_id[s][j] == -2) {
  219.         links_len = j;
  220.         break;
  221.       }
  222.     }
  223.  
  224.     g_links_id[s][links_len] = e;
  225.     g_links_weight[s][links_len] = len;
  226.  
  227.     for (int j=0; j < 100; j++) {
  228.       if (g_links_id[e][j] == -2) {
  229.         links_len = j;
  230.         break;
  231.       }
  232.     }
  233.     g_links_id[e][links_len] = s;
  234.     g_links_weight[e][links_len] = len;
  235.   }
  236. }
  237.  
  238.  
  239. void get_dists_cpp(int a, int L) {
  240.  
  241.  
  242.     int i = L;
  243.     while (--i >= 0) {
  244.        
  245.        
  246.         for (int j = 0; j < 100; j++) {
  247.             //console.log()
  248.             if (g_links_id[i][j] == -2) {
  249.                 break;
  250.             }
  251.             cache_links_id[i][j] = g_links_id[i][j];
  252.             cache_links_weight[i][j] = g_links_weight[i][j];
  253.         }
  254.  
  255.         cache_id[i] = i;
  256.     }
  257.  
  258.  
  259.     queue_id[0] = cache_id[a];
  260.     cache_distance[a] = 0;
  261.     queue_distance[0] = cache_distance[a];
  262.     queue_prev[0] = queue_prev[a];
  263.    
  264.     for (int j=0; j < 100; j++) {
  265.       if (cache_links_id[a][j] == -2) {
  266.         queue_links_id_len[0] = j;
  267.         break;
  268.       }
  269.         queue_links_id[0][j] = cache_links_id[a][j];
  270.         queue_links_weight[0][j] = cache_links_weight[a][j];
  271.     }
  272.    
  273.  
  274.    
  275.     i=0;
  276.     int queue_len = 1;
  277.     while (i < queue_len) {
  278.         int node_id = queue_id[i];
  279.         float node_distance = queue_distance[i];
  280.         int node_prev = queue_prev[i];
  281.      
  282.       /*
  283.         int j=0;
  284.         for (int k=0; k < 100; k++) {
  285.             if (queue_links_id[i][k] == -2) {
  286.                 j=k;
  287.                 break;
  288.             }
  289.         }
  290.         */
  291.        
  292.         int j = queue_links_id_len[i];
  293.  
  294.         while (--j >= 0) {
  295.             int link_id = queue_links_id[i][j];
  296.             float link_weight = queue_links_weight[i][j];
  297.  
  298.             int c_id = cache_id[link_id];
  299.             float c_distance = cache_distance[link_id];
  300.             int c_prev = cache_prev[link_id];
  301.  
  302.             float d = node_distance + link_weight;
  303.             if (d <= c_distance) {
  304.                 cache_prev[link_id] = node_id;
  305.                 cache_distance[link_id] = d;
  306.  
  307.                
  308.                 queue_id[queue_len] = cache_id[link_id];
  309.                 queue_distance[queue_len] = cache_distance[link_id];
  310.  
  311.                 for (int k=0; k < 100; k++) {
  312.                     if (cache_links_id[link_id][k] == -2) {
  313.                       queue_links_id_len[queue_len] = k;
  314.                         break;
  315.                     }
  316.                    
  317.             queue_links_id[queue_len][k] = cache_links_id[link_id][k];
  318.             queue_links_weight[queue_len][k] = cache_links_weight[link_id][k];
  319.           }
  320.           queue_prev[queue_len] = cache_prev[link_id];
  321.           queue_len++;
  322.             }
  323.  
  324.         }
  325.         i++;
  326.     }
  327.  
  328. }
  329.  
  330. int get_edges_start(int index) {
  331.   return edges_start[index];
  332. }
  333.  
  334. float get_cache_distance(int index) {
  335.     return cache_distance[index];
  336. }
  337.  
  338. int get_cache_prev(int index) {
  339.     return cache_prev[index];
  340. }
  341.  
  342.  
  343.  
  344. int main() {
  345.     init_edges_cpp();
  346.     init_cpp(5);
  347.     add_edge_cpp(0, 0, 2, 1);
  348.     add_edge_cpp(1, 0, 1, 1);
  349.     add_edge_cpp(2, 1, 2, 1);
  350.     add_edge_cpp(3, 2, 3, 1);
  351.     add_edge_cpp(4, 2, 4, 1);
  352.     add_edge_cpp(5, 3, 4, 1);
  353.     fill_graph_cpp();
  354.    
  355.     get_dists_cpp(0, 5);
  356.     /*
  357.     for (int i=0; i < 10; i++) {
  358.         cout << i << " " << get_cache_distance(i) << " " << get_cache_prev(i) << endl;
  359.     }
  360.     */
  361.    
  362.     return 0;
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement