括号和按位运算的换行符

问题描述 投票:0回答:1

我提取了这段代码,用于从数据表中组装传感器数据。有什么方法可以使其更具可读性,最好是将某些括号和按位运算分解为新行,而不是弄乱列宽?

var1 = ((((adc_T>>3) – ((BME280_S32_t)dig_T1<<1))) * ((BME280_S32_t)dig_T2)) >> 11;
var2 = (((((adc_T>>4) – ((BME280_S32_t)dig_T1)) * ((adc_T>>4) – ((BME280_S32_t)dig_T1))) >> 12) *
       ((BME280_S32_t)dig_T3)) >> 14;
t_fine = var1 + var2;
T = (t_fine * 5 + 128) >> 8;
c clang-format
1个回答
0
投票

我会转换为更多行并引入更多变量。任何优化编译器都不会创建它们。

在我看来,自动分割这些可怕表达式的最佳工具是 ChatGPT,例如它可以将你的表达式转换为:

BME280_S32_t adc_T_shifted3 = adc_T >> 3;
BME280_S32_t dig_T1_shifted1 = (BME280_S32_t)dig_T1 << 1;

BME280_S32_t var1 = (adc_T_shifted3 - dig_T1_shifted1) * ((BME280_S32_t)dig_T2);
var1 = var1 >> 11;

BME280_S32_t adc_T_shifted4 = adc_T >> 4;
BME280_S32_t diff_T1 = adc_T_shifted4 - (BME280_S32_t)dig_T1;

BME280_S32_t var2 = (diff_T1 * diff_T1) >> 12;
var2 = (var2 * (BME280_S32_t)dig_T3) >> 14;

BME280_S32_t t_fine = var1 + var2;

BME280_S32_t T = (t_fine * 5 + 128) >> 8;

它还可以生成不错的 doxygen 评论:

// Shift raw ADC temperature reading right by 3 bits to reduce precision for intermediate calculations.
// This is part of the scaling required to align the raw ADC value with the calibration constants.
BME280_S32_t adc_T_shifted3 = adc_T >> 3;

// Shift calibration parameter dig_T1 left by 1 bit to double its value.
// This effectively multiplies dig_T1 by 2, which is required for the temperature compensation algorithm.
BME280_S32_t dig_T1_shifted1 = (BME280_S32_t)dig_T1 << 1;

// Calculate var1 as the product of the adjusted temperature difference and calibration parameter dig_T2.
// The result is then shifted right by 11 bits to scale down the value and fit it within the expected range of var1.
// The shift right helps to avoid potential overflows and matches the scaling in the compensation formula.
BME280_S32_t var1 = (adc_T_shifted3 - dig_T1_shifted1) * ((BME280_S32_t)dig_T2);
var1 = var1 >> 11;

// Shift the raw ADC temperature reading right by 4 bits for a different level of precision.
// This shifted value is used in further calculations to compensate for non-linearities.
BME280_S32_t adc_T_shifted4 = adc_T >> 4;

// Calculate the difference between the shifted adc_T value and dig_T1.
// This difference is used to determine the quadratic term in the temperature compensation formula.
BME280_S32_t diff_T1 = adc_T_shifted4 - (BME280_S32_t)dig_T1;

// Calculate var2 by squaring diff_T1 to account for non-linear temperature effects, then shifting right by 12 bits.
// The shift helps reduce the range to avoid overflow and maintain a suitable precision for further calculations.
// Multiply by calibration parameter dig_T3, and then shift right by 14 bits to scale the value appropriately for var2.
BME280_S32_t var2 = (diff_T1 * diff_T1) >> 12;
var2 = (var2 * (BME280_S32_t)dig_T3) >> 14;

// Calculate t_fine as the sum of var1 and var2.
// t_fine is an intermediate variable used for fine-tuning the temperature calculation and is also used in pressure compensation.
BME280_S32_t t_fine = var1 + var2;

// Calculate the final temperature value by multiplying t_fine by 5, adding 128 for rounding purposes, and then shifting right by 8 bits.
// The shift right by 8 bits ensures the final temperature value is scaled correctly (in degrees Celsius, multiplied by 100).
BME280_S32_t T = (t_fine * 5 + 128) >> 8;

PS我知道人工智能答案是不允许的,但它回答了OP问题。 clang-format 对此不够“智能”,我只是想向他展示正确的工具

© www.soinside.com 2019 - 2024. All rights reserved.