浏览代码

math: fix signed int left shift ub in sqrt

Both sqrt and sqrtf shifted the signed exponent as signed int to adjust
the bit representation of the result. There are signed right shifts too
in the code but those are implementation defined and are expected to
compile to arithmetic shift on supported compilers and targets.
Szabolcs Nagy 5 年之前
父节点
当前提交
e858063070
共有 2 个文件被更改,包括 2 次插入4 次删除
  1. 1 2
      src/math/sqrt.c
  2. 1 2
      src/math/sqrtf.c

+ 1 - 2
src/math/sqrt.c

@@ -179,7 +179,6 @@ double sqrt(double x)
 	ix1 = q1>>1;
 	ix1 = q1>>1;
 	if (q&1)
 	if (q&1)
 		ix1 |= sign;
 		ix1 |= sign;
-	ix0 += m << 20;
-	INSERT_WORDS(z, ix0, ix1);
+	INSERT_WORDS(z, ix0 + ((uint32_t)m << 20), ix1);
 	return z;
 	return z;
 }
 }

+ 1 - 2
src/math/sqrtf.c

@@ -78,7 +78,6 @@ float sqrtf(float x)
 		}
 		}
 	}
 	}
 	ix = (q>>1) + 0x3f000000;
 	ix = (q>>1) + 0x3f000000;
-	ix += m << 23;
-	SET_FLOAT_WORD(z, ix);
+	SET_FLOAT_WORD(z, ix + ((uint32_t)m << 23));
 	return z;
 	return z;
 }
 }