From: Kevin Day Date: Wed, 3 Jan 2024 23:22:17 +0000 (-0600) Subject: Update: Optimize bitwise logic regarding removing bits. X-Git-Tag: 0.6.9~44 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=185d78fbf16943ec70b36335c7997ef99f004f65;p=fll Update: Optimize bitwise logic regarding removing bits. 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. --- diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index 19d275c..9aa21ed 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -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; diff --git a/level_1/fl_conversion/c/private-conversion.c b/level_1/fl_conversion/c/private-conversion.c index 5d8439f..30aa464 100644 --- a/level_1/fl_conversion/c/private-conversion.c +++ b/level_1/fl_conversion/c/private-conversion.c @@ -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); diff --git a/level_3/byte_dump/c/byte_dump.c b/level_3/byte_dump/c/byte_dump.c index 92c911d..80e3a1b 100644 --- a/level_3/byte_dump/c/byte_dump.c +++ b/level_3/byte_dump/c/byte_dump.c @@ -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; diff --git a/level_3/controller/c/controller.c b/level_3/controller/c/controller.c index 9c1852d..03f9ba9 100644 --- a/level_3/controller/c/controller.c +++ b/level_3/controller/c/controller.c @@ -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; diff --git a/level_3/controller/c/entry/private-entry.c b/level_3/controller/c/entry/private-entry.c index e687a2c..d21511c 100644 --- a/level_3/controller/c/entry/private-entry.c +++ b/level_3/controller/c/entry/private-entry.c @@ -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 { diff --git a/level_3/controller/c/rule/private-rule.c b/level_3/controller/c/rule/private-rule.c index 96d3179..a0c2428 100644 --- a/level_3/controller/c/rule/private-rule.c +++ b/level_3/controller/c/rule/private-rule.c @@ -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) { diff --git a/level_3/utf8/c/utf8.c b/level_3/utf8/c/utf8.c index 31423c1..d0460e8 100644 --- a/level_3/utf8/c/utf8.c +++ b/level_3/utf8/c/utf8.c @@ -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) {