@@ -296,7 +296,12 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
296296
297297if (GIST_LEAF (entry ))
298298{/* all leafs contains orig trgm */
299- float4 tmpsml = cnt_sml (qtrg ,key ,* recheck );
299+ /*
300+ * Prevent gcc optimizing the tmpsml variable using volatile
301+ * keyword. Otherwise comparison of nlimit and tmpsml may give
302+ * wrong results.
303+ */
304+ float4 volatile tmpsml = cnt_sml (qtrg ,key ,* recheck );
300305
301306/* strange bug at freebsd 5.2.1 and gcc 3.3.3 */
302307res = (* (int * )& tmpsml == * (int * )& nlimit || tmpsml > nlimit );
@@ -471,7 +476,13 @@ gtrgm_distance(PG_FUNCTION_ARGS)
471476* recheck = strategy == SubwordDistanceStrategyNumber ;
472477if (GIST_LEAF (entry ))
473478{/* all leafs contains orig trgm */
474- res = 1.0 - cnt_sml (qtrg ,key ,* recheck );
479+ /*
480+ * Prevent gcc optimizing the sml variable using volatile
481+ * keyword. Otherwise res can differ from the
482+ * subword_similarity_dist_op() function.
483+ */
484+ float4 volatile sml = cnt_sml (qtrg ,key ,* recheck );
485+ res = 1.0 - sml ;
475486}
476487else if (ISALLTRUE (key ))
477488{/* all leafs contains orig trgm */