Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6b1a213

Browse files
committed
New method for preventing compile-time calculation of degree constants.
Commit65abaab tried to prevent the scaling constants used inthe degree-based trig functions from being precomputed at compile time,because some compilers do that with functions that don't yield resultsidentical-to-the-last-bit to what you get at runtime. A report fromPeter Eisentraut suggests that some recent compilers are smart enoughto see through that trick, though. Instead, let's put the inputs tothese calculations into non-const global variables, which should be amore reliable way of convincing the compiler that it can't assume thatthey are compile-time constants. (If we really get desperate, we couldmark these variables "volatile", but I do not believe we should have to.)
1 parent40e89e2 commit6b1a213

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

‎src/backend/utils/adt/float.c

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,24 @@ static float8 atan_1_0 = 0;
7777
staticfloat8tan_45=0;
7878
staticfloat8cot_45=0;
7979

80+
/*
81+
* These are intentionally not static; don't "fix" them. They will never
82+
* be referenced by other files, much less changed; but we don't want the
83+
* compiler to know that, else it might try to precompute expressions
84+
* involving them. See comments for init_degree_constants().
85+
*/
86+
float8degree_c_thirty=30.0;
87+
float8degree_c_forty_five=45.0;
88+
float8degree_c_sixty=60.0;
89+
float8degree_c_one_half=0.5;
90+
float8degree_c_one=1.0;
91+
8092
/* Local function prototypes */
8193
staticintfloat4_cmp_internal(float4a,float4b);
8294
staticintfloat8_cmp_internal(float8a,float8b);
8395
staticdoublesind_q1(doublex);
8496
staticdoublecosd_q1(doublex);
85-
86-
/* This is INTENTIONALLY NOT STATIC. Don't "fix" it. */
87-
voidinit_degree_constants(float8thirty,float8forty_five,float8sixty,
88-
float8one_half,float8one);
97+
staticvoidinit_degree_constants(void);
8998

9099
#ifndefHAVE_CBRT
91100
/*
@@ -1814,35 +1823,31 @@ dtan(PG_FUNCTION_ARGS)
18141823
* compilers out there that will precompute expressions such as sin(constant)
18151824
* using a sin() function different from what will be used at runtime. If we
18161825
* want exact results, we must ensure that none of the scaling constants used
1817-
* in the degree-based trig functions are computed that way.
1818-
*
1819-
* The whole approach fails if init_degree_constants() gets inlined into the
1820-
* call sites, since then constant-folding can happen anyway. Currently it
1821-
* seems sufficient to declare it non-static to prevent that. We have no
1822-
* expectation that other files will call this, but don't tell gcc that.
1826+
* in the degree-based trig functions are computed that way. To do so, we
1827+
* compute them from the variables degree_c_thirty etc, which are also really
1828+
* constants, but the compiler cannot assume that.
18231829
*
18241830
* Other hazards we are trying to forestall with this kluge include the
18251831
* possibility that compilers will rearrange the expressions, or compute
18261832
* some intermediate results in registers wider than a standard double.
18271833
*/
1828-
void
1829-
init_degree_constants(float8thirty,float8forty_five,float8sixty,
1830-
float8one_half,float8one)
1831-
{
1832-
sin_30=sin(thirty*RADIANS_PER_DEGREE);
1833-
one_minus_cos_60=1.0-cos(sixty*RADIANS_PER_DEGREE);
1834-
asin_0_5=asin(one_half);
1835-
acos_0_5=acos(one_half);
1836-
atan_1_0=atan(one);
1837-
tan_45=sind_q1(forty_five) /cosd_q1(forty_five);
1838-
cot_45=cosd_q1(forty_five) /sind_q1(forty_five);
1834+
staticvoid
1835+
init_degree_constants(void)
1836+
{
1837+
sin_30=sin(degree_c_thirty*RADIANS_PER_DEGREE);
1838+
one_minus_cos_60=1.0-cos(degree_c_sixty*RADIANS_PER_DEGREE);
1839+
asin_0_5=asin(degree_c_one_half);
1840+
acos_0_5=acos(degree_c_one_half);
1841+
atan_1_0=atan(degree_c_one);
1842+
tan_45=sind_q1(degree_c_forty_five) /cosd_q1(degree_c_forty_five);
1843+
cot_45=cosd_q1(degree_c_forty_five) /sind_q1(degree_c_forty_five);
18391844
degree_consts_set= true;
18401845
}
18411846

18421847
#defineINIT_DEGREE_CONSTANTS() \
18431848
do { \
18441849
if (!degree_consts_set) \
1845-
init_degree_constants(30.0, 45.0, 60.0, 0.5, 1.0); \
1850+
init_degree_constants(); \
18461851
} while(0)
18471852

18481853

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp