static tree
tree_fold_binomial (tree type, tree n, unsigned int k)
{
- double_int num, denom, idx, di_res;
bool overflow;
unsigned int i;
tree res;
return fold_convert (type, n);
/* Numerator = n. */
- num = TREE_INT_CST (n);
+ wide_int num = n;
/* Check that k <= n. */
- if (num.ult (double_int::from_uhwi (k)))
+ if (wi::ltu_p (num, k))
return NULL_TREE;
/* Denominator = 2. */
- denom = double_int::from_uhwi (2);
+ wide_int denom = wi::two (TYPE_PRECISION (TREE_TYPE (n)));
/* Index = Numerator-1. */
- idx = num - double_int_one;
+ wide_int idx = num - 1;
/* Numerator = Numerator*Index = n*(n-1). */
- num = num.mul_with_sign (idx, false, &overflow);
+ num = wi::smul (num, idx, &overflow);
if (overflow)
return NULL_TREE;
--idx;
/* Numerator *= Index. */
- num = num.mul_with_sign (idx, false, &overflow);
+ num = wi::smul (num, idx, &overflow);
if (overflow)
return NULL_TREE;
/* Denominator *= i. */
- denom *= double_int::from_uhwi (i);
+ denom *= i;
}
/* Result = Numerator / Denominator. */
- di_res = num.div (denom, true, EXACT_DIV_EXPR);
- res = build_int_cst_wide (type, di_res.low, di_res.high);
+ wide_int di_res = wi::udiv_trunc (num, denom);
+ res = wide_int_to_tree (type, di_res);
return int_fits_type_p (res, type) ? res : NULL_TREE;
}