]> Kevux Git Server - fll/commitdiff
Update: Optimize bitwise logic regarding removing bits.
authorKevin Day <thekevinday@gmail.com>
Wed, 3 Jan 2024 23:22:17 +0000 (17:22 -0600)
committerKevin Day <thekevinday@gmail.com>
Wed, 3 Jan 2024 23:22:17 +0000 (17:22 -0600)
I intended to eventually do this and I finally got around to it.
Change the logic to use "x &= ~y" rather than "x -= x & y" or "if (x | y) x -= y".

In the case of "x -= x & y", the resulting object code is identical in my tests.
However, the code is simpler to write with the "x &= ~y".

The "if (x | y) x -= y" results in a lot more commands in the object.

level_0/f_file/c/file.c
level_1/fl_conversion/c/private-conversion.c
level_3/byte_dump/c/byte_dump.c
level_3/controller/c/controller.c
level_3/controller/c/entry/private-entry.c
level_3/controller/c/rule/private-rule.c
level_3/utf8/c/utf8.c

index 19d275cfad7aac02cd16de601d79ce2ef6ab2772..9aa21ed14c468582f280ad2128767b89d2217492 100644 (file)
@@ -641,36 +641,24 @@ extern "C" {
 
       if (mode_change & F_file_mode_t_block_special_d) {
         if (change & F_file_mode_t_mask_bit_set_owner_d & F_file_mode_t_mask_how_subtract_d) {
-          if (*mode & F_file_mode_special_set_user_d) {
-            *mode -= F_file_mode_special_set_user_d;
-          }
+          *mode &= ~F_file_mode_special_set_user_d;
         }
         else if (change & F_file_mode_t_mask_bit_set_owner_d & F_file_mode_t_mask_how_add_d) {
-          if (!(*mode & F_file_mode_special_set_user_d)) {
-            *mode |= F_file_mode_special_set_user_d;
-          }
+          *mode |= F_file_mode_special_set_user_d;
         }
 
         if (change & F_file_mode_t_mask_bit_set_group_d & F_file_mode_t_mask_how_subtract_d) {
-          if (*mode & F_file_mode_special_set_group_d) {
-            *mode -= F_file_mode_special_set_group_d;
-          }
+          *mode &= ~F_file_mode_special_set_group_d;
         }
         else if (change & F_file_mode_t_mask_bit_set_group_d & F_file_mode_t_mask_how_add_d) {
-          if (!(*mode & F_file_mode_special_set_group_d)) {
-            *mode |= F_file_mode_special_set_group_d;
-          }
+          *mode |= F_file_mode_special_set_group_d;
         }
 
         if (change & F_file_mode_t_mask_bit_sticky_d & F_file_mode_t_mask_how_subtract_d) {
-          if (*mode & F_file_mode_special_sticky_d) {
-            *mode -= F_file_mode_special_sticky_d;
-          }
+          *mode &= ~F_file_mode_special_sticky_d;
         }
         else if (change & F_file_mode_t_mask_bit_sticky_d & F_file_mode_t_mask_how_add_d) {
-          if (!(*mode & F_file_mode_special_sticky_d)) {
-            *mode |= F_file_mode_special_sticky_d;
-          }
+          *mode |= F_file_mode_special_sticky_d;
         }
       }
     }
@@ -700,54 +688,34 @@ extern "C" {
 
       if (mode_change & F_file_mode_t_block_owner_d) {
         if (change & F_file_mode_t_mask_bit_read_d & F_file_mode_t_mask_how_subtract_d) {
-          if (*mode & F_file_mode_owner_r_d) {
-            *mode -= F_file_mode_owner_r_d;
-          }
+          *mode &= ~F_file_mode_owner_r_d;
         }
         else if (change & F_file_mode_t_mask_bit_read_d & F_file_mode_t_mask_how_add_d) {
-          if (!(*mode & F_file_mode_owner_r_d)) {
-            *mode |= F_file_mode_owner_r_d;
-          }
+          *mode |= F_file_mode_owner_r_d;
         }
 
         if (change & F_file_mode_t_mask_bit_write_d & F_file_mode_t_mask_how_subtract_d) {
-          if (*mode & F_file_mode_owner_w_d) {
-            *mode -= F_file_mode_owner_w_d;
-          }
+          *mode &= ~F_file_mode_owner_w_d;
         }
         else if (change & F_file_mode_t_mask_bit_write_d & F_file_mode_t_mask_how_add_d) {
-          if (!(*mode & F_file_mode_owner_w_d)) {
-            *mode |= F_file_mode_owner_w_d;
-          }
+          *mode |= F_file_mode_owner_w_d;
         }
 
         if (change & F_file_mode_t_mask_bit_execute_d) {
-          change &= F_file_mode_t_mask_bit_execute_d;
-
           if (change & F_file_mode_t_mask_how_subtract_d) {
-            if (*mode & F_file_mode_owner_x_d) {
-              *mode -= F_file_mode_owner_x_d;
-            }
+            *mode &= ~F_file_mode_owner_x_d;
           }
           else if (change & F_file_mode_t_mask_how_add_d) {
-            if (!(*mode & F_file_mode_owner_x_d)) {
-              *mode |= F_file_mode_owner_x_d;
-            }
+            *mode |= F_file_mode_owner_x_d;
           }
         }
         else if (change & F_file_mode_t_mask_bit_execute_only_d) {
-          change &= F_file_mode_t_mask_bit_execute_only_d;
-
           if (directory_is || (mode_file & F_file_mode_owner_x_d)) {
             if (change & F_file_mode_t_mask_how_subtract_d) {
-              if (*mode & F_file_mode_owner_x_d) {
-                *mode -= F_file_mode_owner_x_d;
-              }
+              *mode &= ~F_file_mode_owner_x_d;
             }
             else if (change & F_file_mode_t_mask_how_add_d) {
-              if (!(*mode & F_file_mode_owner_x_d)) {
-                *mode |= F_file_mode_owner_x_d;
-              }
+              *mode |= F_file_mode_owner_x_d;
             }
           }
         }
@@ -779,54 +747,34 @@ extern "C" {
 
       if (mode_change & F_file_mode_t_block_group_d) {
         if (change & F_file_mode_t_mask_bit_read_d & F_file_mode_t_mask_how_subtract_d) {
-          if (*mode & F_file_mode_group_r_d) {
-            *mode -= F_file_mode_group_r_d;
-          }
+          *mode &= ~F_file_mode_group_r_d;
         }
         else if (change & F_file_mode_t_mask_bit_read_d & F_file_mode_t_mask_how_add_d) {
-          if (!(*mode & F_file_mode_group_r_d)) {
-            *mode |= F_file_mode_group_r_d;
-          }
+          *mode |= F_file_mode_group_r_d;
         }
 
         if (change & F_file_mode_t_mask_bit_write_d & F_file_mode_t_mask_how_subtract_d) {
-          if (*mode & F_file_mode_group_w_d) {
-            *mode -= F_file_mode_group_w_d;
-          }
+          *mode &= ~F_file_mode_group_w_d;
         }
         else if (change & F_file_mode_t_mask_bit_write_d & F_file_mode_t_mask_how_add_d) {
-          if (!(*mode & F_file_mode_group_w_d)) {
-            *mode |= F_file_mode_group_w_d;
-          }
+          *mode |= F_file_mode_group_w_d;
         }
 
         if (change & F_file_mode_t_mask_bit_execute_d) {
-          change &= F_file_mode_t_mask_bit_execute_d;
-
           if (change & F_file_mode_t_mask_how_subtract_d) {
-            if (*mode & F_file_mode_group_x_d) {
-              *mode -= F_file_mode_group_x_d;
-            }
+            *mode &= ~F_file_mode_group_x_d;
           }
           else if (change & F_file_mode_t_mask_how_add_d) {
-            if (!(*mode & F_file_mode_group_x_d)) {
-              *mode |= F_file_mode_group_x_d;
-            }
+            *mode |= F_file_mode_group_x_d;
           }
         }
         else if (change & F_file_mode_t_mask_bit_execute_only_d) {
-          change &= F_file_mode_t_mask_bit_execute_only_d;
-
           if (directory_is || (mode_file & F_file_mode_group_x_d)) {
             if (change & F_file_mode_t_mask_how_subtract_d) {
-              if (*mode & F_file_mode_group_x_d) {
-                *mode -= F_file_mode_group_x_d;
-              }
+              *mode &= ~F_file_mode_group_x_d;
             }
             else if (change & F_file_mode_t_mask_how_add_d) {
-              if (!(*mode & F_file_mode_group_x_d)) {
-                *mode |= F_file_mode_group_x_d;
-              }
+              *mode |= F_file_mode_group_x_d;
             }
           }
         }
@@ -858,20 +806,14 @@ extern "C" {
 
       if (mode_change & F_file_mode_t_block_world_d) {
         if (change & F_file_mode_t_mask_bit_read_d & F_file_mode_t_mask_how_subtract_d) {
-          if (*mode & F_file_mode_world_r_d) {
-            *mode -= F_file_mode_world_r_d;
-          }
+          *mode &= ~F_file_mode_world_r_d;
         }
         else if (change & F_file_mode_t_mask_bit_read_d & F_file_mode_t_mask_how_add_d) {
-          if (!(*mode & F_file_mode_world_r_d)) {
-            *mode |= F_file_mode_world_r_d;
-          }
+          *mode |= F_file_mode_world_r_d;
         }
 
         if (change & F_file_mode_t_mask_bit_write_d & F_file_mode_t_mask_how_subtract_d) {
-          if (*mode & F_file_mode_world_w_d) {
-            *mode -= F_file_mode_world_w_d;
-          }
+          *mode &= ~F_file_mode_world_w_d;
         }
         else if (change & F_file_mode_t_mask_bit_write_d & F_file_mode_t_mask_how_add_d) {
           if (!(*mode & F_file_mode_world_w_d)) {
@@ -880,32 +822,20 @@ extern "C" {
         }
 
         if (change & F_file_mode_t_mask_bit_execute_d) {
-          change &= F_file_mode_t_mask_bit_execute_d;
-
           if (change & F_file_mode_t_mask_how_subtract_d) {
-            if (*mode & F_file_mode_world_x_d) {
-              *mode -= F_file_mode_world_x_d;
-            }
+            *mode &= ~F_file_mode_world_x_d;
           }
           else if (change & F_file_mode_t_mask_how_add_d) {
-            if (!(*mode & F_file_mode_world_x_d)) {
-              *mode |= F_file_mode_world_x_d;
-            }
+            *mode |= F_file_mode_world_x_d;
           }
         }
         else if (change & F_file_mode_t_mask_bit_execute_only_d) {
-          change &= F_file_mode_t_mask_bit_execute_only_d;
-
           if (directory_is || (mode_file & F_file_mode_world_x_d)) {
             if (change & F_file_mode_t_mask_how_subtract_d) {
-              if (*mode & F_file_mode_world_x_d) {
-                *mode -= F_file_mode_world_x_d;
-              }
+              *mode &= ~F_file_mode_world_x_d;
             }
             else if (change & F_file_mode_t_mask_how_add_d) {
-              if (!(*mode & F_file_mode_world_x_d)) {
-                *mode |= F_file_mode_world_x_d;
-              }
+              *mode |= F_file_mode_world_x_d;
             }
           }
         }
@@ -1044,7 +974,7 @@ extern "C" {
 
             // Clear by mask to prepare for replacement, which includes clearing the special block.
             mode_mask |= F_file_mode_t_block_special_d;
-            mode_result -= mode_result & mode_mask;
+            mode_result &= ~mode_mask;
 
             replace_result |= F_file_mode_t_replace_special_d;
 
@@ -1115,7 +1045,7 @@ extern "C" {
               incomplete = F_false;
 
               if (how > 3) {
-                mode_result -= mode_result & mode_umask;
+                mode_result &= ~mode_umask;
               }
 
               on = 0;
@@ -1138,7 +1068,7 @@ extern "C" {
 
                 // Clear by mask to prepare for replacement, which includes clearing the special block.
                 mode_mask |= F_file_mode_t_block_special_d;
-                mode_result -= mode_result & mode_mask;
+                mode_result &= ~mode_mask;
 
                 replace_result |= F_file_mode_t_replace_special_d;
 
@@ -1165,25 +1095,19 @@ extern "C" {
               if (how == 1 || how == 2 || how == 4 || how == 5) {
                 incomplete = F_false;
                 mode_result |= what & mode_mask & F_file_mode_t_mask_how_add_d;
-
-                if (mode_result & what & mode_mask & F_file_mode_t_mask_how_subtract_d) {
-                  mode_result -= mode_result & what & mode_mask & F_file_mode_t_mask_how_subtract_d;
-                }
+                mode_result &= ~F_file_mode_t_mask_how_subtract_d;
               }
               else if (how == 3 || how == 6) {
                 incomplete = F_false;
                 mode_result |= what & mode_mask & F_file_mode_t_mask_how_subtract_d;
-
-                if (mode_result & what & mode_mask & F_file_mode_t_mask_how_add_d) {
-                  mode_result -= mode_result & what & mode_mask & F_file_mode_t_mask_how_add_d;
-                }
+                mode_result &= ~F_file_mode_t_mask_how_add_d;
               }
             }
           } // for
 
           if (how > 3) {
             incomplete = F_false;
-            mode_result -= mode_result & mode_umask;
+            mode_result &= ~mode_umask;
           }
 
           if (!code.string[i]) break;
index 5d8439f70395afa42df837e98fd6846361dfcd5b..30aa464b67f460da7a017dfc4e74717743294b70 100644 (file)
@@ -455,8 +455,8 @@ extern "C" {
     if (vector == -1) {
       data.flag |= FL_conversion_data_flag_negative_d;
     }
-    else if (data.flag & FL_conversion_data_flag_negative_d) {
-      data.flag -= FL_conversion_data_flag_negative_d;
+    else {
+      data.flag &= ~FL_conversion_data_flag_negative_d;
     }
 
     if (mode == 10 || mode == 16 || mode == 12 || mode == 8) {
@@ -596,9 +596,7 @@ extern "C" {
 
     fl_conversion_data_t data = macro_fl_conversion_data_t_initialize(mode, flag);
 
-    if (data.flag & FL_conversion_data_flag_negative_d) {
-      data.flag -= FL_conversion_data_flag_negative_d;
-    }
+    data.flag &= ~FL_conversion_data_flag_negative_d;
 
     if (mode == 10 || mode == 16 || mode == 12 || mode == 8) {
       status = private_fl_conversion_dynamic_to_base_unsigned(data, string + offset, length - offset, number);
index 92c911dd583f032764d2405bcaeb73ca380a5f79..80e3a1b062e0426933b9b8604dde2f144d659047 100644 (file)
@@ -254,9 +254,7 @@ extern "C" {
       }
 
       if (choice == byte_dump_parameter_narrow_e) {
-        if (data.options & byte_dump_option_wide_d) {
-          data.options -= byte_dump_option_wide_d;
-        }
+        data.options &= ~byte_dump_option_wide_d;
       }
       else if (choice == byte_dump_parameter_wide_e) {
         data.options |= byte_dump_option_wide_d;
index 9c1852def46ffa7893dcf3533f131555c8919f2d..03f9ba92f76715f052891fef5217a34e37812724 100644 (file)
@@ -395,15 +395,13 @@ extern "C" {
       if (main->parameters.array[controller_parameter_interruptible_e].result == f_console_result_found_e) {
         setting.flag |= controller_setting_flag_interruptible_e;
       }
-      else if (setting.flag & controller_setting_flag_interruptible_e) {
-        setting.flag -= controller_setting_flag_interruptible_e;
+      else {
+        setting.flag &= ~controller_setting_flag_interruptible_e;
       }
     }
     else {
       if (main->parameters.array[controller_parameter_uninterruptible_e].result == f_console_result_found_e) {
-        if (setting.flag & controller_setting_flag_interruptible_e) {
-          setting.flag -= controller_setting_flag_interruptible_e;
-        }
+        setting.flag &= ~controller_setting_flag_interruptible_e;
       }
       else {
         setting.flag |= controller_setting_flag_interruptible_e;
index e687a2c3db86aa1b9ebce5a1a8c432308322a246..d21511cefc20d874abdd375413e9675ae31889d9 100644 (file)
@@ -526,9 +526,7 @@ extern "C" {
 
             if (action->status == F_none) {
               if (action->parameters.used == 2) {
-                if (action->flag & controller_entry_action_flag_undefined_e) {
-                  action->flag -= controller_entry_action_flag_undefined_e;
-                }
+                action->flag &= ~controller_entry_action_flag_undefined_e;
 
                 status = fl_conversion_dynamic_to_unsigned_detect(fl_conversion_data_base_10_c, action->parameters.array[1], &action->number);
 
@@ -1811,9 +1809,7 @@ extern "C" {
                   for (k = 0; k < entry->items.used; ++k) {
 
                     if (fl_string_dynamic_compare(action->parameters.array[0], entry->items.array[k].name) == F_equal_to) {
-                      if (missing & 0x1) {
-                        missing -= 0x1;
-                      }
+                      missing &= ~0x1;
 
                       break;
                     }
@@ -1970,9 +1966,7 @@ extern "C" {
           }
         }
         else {
-          if (global.setting->control.flag & controller_control_flag_readonly_e) {
-            global.setting->control.flag -= controller_control_flag_readonly_e;
-          }
+          global.setting->control.flag &= ~controller_control_flag_readonly_e;
         }
 
         cache->action.generic.used = 0;
@@ -2244,9 +2238,7 @@ extern "C" {
             continue;
           }
 
-          if (entry->flag & controller_entry_flag_timeout_exit_no_e) {
-            entry->flag -= controller_entry_flag_timeout_exit_no_e;
-          }
+          entry->flag &= ~controller_entry_flag_timeout_exit_no_e;
 
           time = &entry->timeout_exit;
         }
@@ -2257,10 +2249,7 @@ extern "C" {
             continue;
           }
 
-          if (entry->flag & controller_entry_flag_timeout_kill_no_e) {
-            entry->flag -= controller_entry_flag_timeout_kill_no_e;
-          }
-
+          entry->flag &= ~controller_entry_flag_timeout_kill_no_e;
           time = &entry->timeout_kill;
         }
         else if (fl_string_dynamic_partial_compare_string(controller_start_s.string, cache->buffer_file, controller_start_s.used, cache->content_actions.array[i].array[0]) == F_equal_to) {
@@ -2270,10 +2259,7 @@ extern "C" {
             continue;
           }
 
-          if (entry->flag & controller_entry_flag_timeout_start_no_e) {
-            entry->flag -= controller_entry_flag_timeout_start_no_e;
-          }
-
+          entry->flag &= ~controller_entry_flag_timeout_start_no_e;
           time = &entry->timeout_start;
         }
         else if (fl_string_dynamic_partial_compare_string(controller_stop_s.string, cache->buffer_file, controller_stop_s.used, cache->content_actions.array[i].array[0]) == F_equal_to) {
@@ -2283,10 +2269,7 @@ extern "C" {
             continue;
           }
 
-          if (entry->flag & controller_entry_flag_timeout_stop_no_e) {
-            entry->flag -= controller_entry_flag_timeout_stop_no_e;
-          }
-
+          entry->flag &= ~controller_entry_flag_timeout_stop_no_e;
           time = &entry->timeout_stop;
         }
         else {
index 96d31794a328fad0b0ce7486ba759d763849e08f..a0c242861676313621a02558be955236f954d4ab 100644 (file)
@@ -535,17 +535,13 @@ extern "C" {
               item->with |= controller_with_session_new_d;
 
               // The "session_new" and "session_same" are mutually exclusive.
-              if (item->with & controller_with_session_same_d) {
-                item->with -= controller_with_session_same_d;
-              }
+              item->with &= ~controller_with_session_same_d;
             }
             else if (fl_string_dynamic_partial_compare_string(controller_session_same_s.string, cache->buffer_item, controller_session_same_s.used, cache->content_action.array[i]) == F_equal_to) {
               item->with |= controller_with_session_same_d;
 
               // The "session_new" and "session_same" are mutually exclusive.
-              if (item->with & controller_with_session_new_d) {
-                item->with -= controller_with_session_new_d;
-              }
+              item->with &= ~controller_with_session_new_d;
             }
             else {
               if (global.main->error.verbosity != f_console_verbosity_quiet_e) {
index 31423c133ded06ead645da0edcc60b6b4e2a2c12..d0460e82f783fa62632c431830d529656d003f14 100644 (file)
@@ -196,17 +196,11 @@ extern "C" {
       }
 
       if (choice == utf8_parameter_from_bytesequence_e) {
-        if (data.mode & utf8_mode_from_codepoint_d) {
-          data.mode -= utf8_mode_from_codepoint_d;
-        }
-
+        data.mode &= ~utf8_mode_from_codepoint_d;
         data.mode |= utf8_mode_from_bytesequence_d;
       }
       else if (choice == utf8_parameter_from_codepoint_e) {
-        if (data.mode & utf8_mode_from_bytesequence_d) {
-          data.mode -= utf8_mode_from_bytesequence_d;
-        }
-
+        data.mode &= ~utf8_mode_from_bytesequence_d;
         data.mode |= utf8_mode_from_codepoint_d;
       }
     }
@@ -254,43 +248,15 @@ extern "C" {
       }
 
       if (choice == utf8_parameter_to_bytesequence_e) {
-        if (data.mode & utf8_mode_to_codepoint_d) {
-          data.mode -= utf8_mode_to_codepoint_d;
-        }
-
-        if (data.mode & utf8_mode_to_combining_d) {
-          data.mode -= utf8_mode_to_combining_d;
-        }
-
-        if (data.mode & utf8_mode_to_width_d) {
-          data.mode -= utf8_mode_to_width_d;
-        }
-
+        data.mode &= ~(utf8_mode_to_codepoint_d | utf8_mode_to_combining_d | utf8_mode_to_width_d);
         data.mode |= utf8_mode_to_bytesequence_d;
       }
       else if (choice == utf8_parameter_to_codepoint_e) {
-        if (data.mode & utf8_mode_to_bytesequence_d) {
-          data.mode -= utf8_mode_to_bytesequence_d;
-        }
-
-        if (data.mode & utf8_mode_to_combining_d) {
-          data.mode -= utf8_mode_to_combining_d;
-        }
-
-        if (data.mode & utf8_mode_to_width_d) {
-          data.mode -= utf8_mode_to_width_d;
-        }
-
+        data.mode &= ~(utf8_mode_to_bytesequence_d | utf8_mode_to_combining_d | utf8_mode_to_width_d);
         data.mode |= utf8_mode_to_codepoint_d;
       }
       else if (choice == utf8_parameter_to_combining_e) {
-        if (data.mode & utf8_mode_to_bytesequence_d) {
-          data.mode -= utf8_mode_to_bytesequence_d;
-        }
-
-        if (data.mode & utf8_mode_to_codepoint_d) {
-          data.mode -= utf8_mode_to_codepoint_d;
-        }
+        data.mode &= ~(utf8_mode_to_bytesequence_d | utf8_mode_to_codepoint_d);
 
         // --to_width may be specified with --to_combining.
         if (main->parameters.array[utf8_parameter_to_width_e].result == f_console_result_found_e) {
@@ -300,13 +266,7 @@ extern "C" {
         data.mode |= utf8_mode_to_combining_d;
       }
       else if (choice == utf8_parameter_to_width_e) {
-        if (data.mode & utf8_mode_to_bytesequence_d) {
-          data.mode -= utf8_mode_to_bytesequence_d;
-        }
-
-        if (data.mode & utf8_mode_to_codepoint_d) {
-          data.mode -= utf8_mode_to_codepoint_d;
-        }
+        data.mode &= ~(utf8_mode_to_bytesequence_d | utf8_mode_to_codepoint_d);
 
         // --to_width may be specified with --to_combining.
         if (main->parameters.array[utf8_parameter_to_combining_e].result == f_console_result_found_e) {