Advertisement
wowonline

Untitled

Dec 12th, 2021
749
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <inttypes.h>
  2. #include <stdio.h>
  3.  
  4. int64_t
  5. min(int64_t f, int64_t s)
  6. {
  7.     return (f < s ? f : s);
  8. }
  9.  
  10. int64_t
  11. calc_max_size(int64_t block_size, int64_t block_num_size, int64_t inode_direct_block_count)
  12. {
  13.     int64_t first = 1, second, block_ratio = block_size / block_num_size;
  14.     int64_t ratio_squared, ratio_cubed;
  15.     int64_t first_overflowed = 0;
  16.     int64_t second_overflowed = 0;
  17.  
  18.     //first expression
  19.     int64_t power = block_num_size * 8;
  20.     if (power >= 64) {
  21.         first_overflowed = 1;
  22.     } else {
  23.         first = first << power;
  24.         if (__builtin_smulll_overflow(first, block_size, &first)) {
  25.             first_overflowed = 1;
  26.         }
  27.     }
  28.    
  29.     //second expression
  30.     if (__builtin_smulll_overflow(block_ratio, block_ratio, &ratio_squared)) {
  31.         second_overflowed = 1;
  32.     }
  33.     if (__builtin_smulll_overflow(ratio_squared, block_ratio, &ratio_cubed)) {
  34.         second_overflowed = 1;
  35.     }
  36.     if (__builtin_saddll_overflow(inode_direct_block_count, block_ratio, &second)) {
  37.         second_overflowed = 1;
  38.     }
  39.     if (__builtin_saddll_overflow(second, ratio_squared, &second)) {
  40.         second_overflowed = 1;
  41.     }
  42.     if (__builtin_saddll_overflow(second, ratio_cubed, &second)) {
  43.         second_overflowed = 1;
  44.     }
  45.     if (__builtin_smulll_overflow(second, block_size, &second)) {
  46.         second_overflowed = 1;
  47.     }
  48.  
  49.     if (first_overflowed && second_overflowed) {
  50.         return -1;
  51.     } else if (!first_overflowed && !second_overflowed) {
  52.         return min(first, second);
  53.     } else if (first_overflowed && !second_overflowed) {
  54.         return second;
  55.     } else {
  56.         return first;
  57.     }
  58. }
  59.  
  60. int
  61. main(int argc, char *argv[])
  62. {
  63.     int64_t block_size, block_num_size, inode_direct_block_count;
  64.     int64_t output, i, max = 0, i_saved = 0;
  65.     scanf("%" SCNd64, &block_size);
  66.     scanf("%" SCNd64, &block_num_size);
  67.     scanf("%" SCNd64, &inode_direct_block_count);
  68.     output = calc_max_size(block_size, block_num_size, inode_direct_block_count);
  69.     printf("%" PRId64 "\n", output);
  70.  
  71.     for (i = 1; i <= block_size; ++i) {
  72.         output = calc_max_size(block_size, i, inode_direct_block_count);
  73.         if (output == -1) {
  74.             printf("%" PRId64 "%" PRId64 "\n", i, output);
  75.         } else if (output > max) {
  76.             max = output;
  77.             i_saved = i;
  78.         }
  79.     }
  80.    
  81.     printf("%" PRId64 "% " PRId64 "\n", i_saved, max);
  82.     return 0;
  83. }
Advertisement
RAW Paste Data Copied
Advertisement