/** * clay_util_relation_negate_row function: * Negate the line at `row' (doesn't affect the e/i column) * \param[in,out] statement * \param[in] row row to negate */ void clay_util_relation_negate_row(osl_relation_p r, int row) { int i; int precision = r->precision; for (i = 1 ; i < r->nb_columns ; i++) { osl_int_oppose(precision, &r->m[row][i], r->m[row][i]); } osl_int_decrement(precision, &r->m[row][r->nb_columns-1], r->m[row][r->nb_columns-1]); }
// Converts relation into ``input form'': coefficients on the input side of the // relation matrix are set to 1 whenever possible. int clay_relation_input_form(osl_relation_p relation) { int i, j, col_i; clay_array_p row_indices = clay_array_malloc(); clay_relation_alpha_equation_rows(row_indices, relation); if (row_indices->size != relation->nb_input_dims) { CLAY_debug("Relation defined by an overdetermined matrix"); return -1; } // Forward iteration (triangularization). for (i = 0; i < row_indices->size; ++i) { col_i = 1 + relation->nb_output_dims + i; for (j = i + 1; j < row_indices->size; ++j) { clay_relation_zero_coefficient(relation, row_indices->data[i], row_indices->data[j], col_i); } } // Reverse iteration (diagonalization). for (i = row_indices->size - 1; i >= 0; --i) { col_i = 1 + relation->nb_output_dims + i; for (j = i - 1; j >= 0; --j) { clay_relation_zero_coefficient(relation, row_indices->data[i], row_indices->data[j], col_i); } } // Change signs on the diagonal. for (i = 0; i < row_indices->size; ++i) { col_i = 1 + relation->nb_output_dims + i; if (osl_int_neg(relation->precision, relation->m[row_indices->data[i]][col_i])) { for (j = 1; j < relation->nb_columns; j++) { osl_int_oppose(relation->precision, &relation->m[row_indices->data[i]][j], relation->m[row_indices->data[i]][j]); } } } clay_array_free(row_indices); return 0; }
int main(int argc, char** argv) { if (argc > 1) { printf("argv are ignored\n"); } unsigned int nb_fail = 0; #ifdef OSL_GMP_IS_HERE int i; for (i = SCHAR_MIN; i <= SCHAR_MAX; ++i) { osl_int_t a_sp; osl_int_init_set_si(OSL_PRECISION_SP, &a_sp, i); osl_int_t a_dp; osl_int_init_set_si(OSL_PRECISION_DP, &a_dp, i); osl_int_t a_mp; osl_int_init_set_si(OSL_PRECISION_MP, &a_mp, i); int j; for (j = SCHAR_MIN; j <= SCHAR_MAX; ++j) { int error = 0; osl_int_t b_sp; osl_int_init_set_si(OSL_PRECISION_SP, &b_sp, j); osl_int_t b_dp; osl_int_init_set_si(OSL_PRECISION_DP, &b_dp, j); osl_int_t b_mp; osl_int_init_set_si(OSL_PRECISION_MP, &b_mp, j); osl_int_t c_sp; osl_int_init(OSL_PRECISION_SP, &c_sp); osl_int_t c_dp; osl_int_init(OSL_PRECISION_DP, &c_dp); osl_int_t c_mp; osl_int_init(OSL_PRECISION_MP, &c_mp); int const a_sp_i = osl_int_get_si(OSL_PRECISION_SP, a_sp); int const a_dp_i = osl_int_get_si(OSL_PRECISION_DP, a_dp); int const a_mp_i = osl_int_get_si(OSL_PRECISION_MP, a_mp); int const b_sp_i = osl_int_get_si(OSL_PRECISION_SP, b_sp); int const b_dp_i = osl_int_get_si(OSL_PRECISION_DP, b_dp); int const b_mp_i = osl_int_get_si(OSL_PRECISION_MP, b_mp); // osl_int_init_set_si & osl_int_init & osl_int_get_si if (!error) { int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (a_sp_i != a_dp_i || a_sp_i != a_mp_i) { error++; printf("Error osl_int_init_set_si or osl_int_get_si\n"); } if (b_sp_i != b_dp_i || b_sp_i != b_mp_i) { error++; printf("Error osl_int_init_set_si or osl_int_get_si\n"); } if (c_sp_i != c_dp_i || c_sp_i != c_mp_i || c_sp_i != 0) { error++; printf("Error osl_int_init or osl_int_get_si\n"); } } // osl_int_assign if (!error) { osl_int_assign(OSL_PRECISION_SP, &c_sp, b_sp); osl_int_assign(OSL_PRECISION_DP, &c_dp, b_dp); osl_int_assign(OSL_PRECISION_MP, &c_mp, b_mp); if (osl_int_ne(OSL_PRECISION_SP, c_sp, b_sp)) { error++; printf("Error osl_int_assign\n"); } if (osl_int_ne(OSL_PRECISION_DP, c_dp, b_dp)) { error++; printf("Error osl_int_assign\n"); } if (osl_int_ne(OSL_PRECISION_MP, c_mp, b_mp)) { error++; printf("Error osl_int_assign\n"); } } // osl_int_swap // osl_int_increment if (!error) { osl_int_increment(OSL_PRECISION_SP, &c_sp, a_sp); osl_int_increment(OSL_PRECISION_DP, &c_dp, a_dp); osl_int_increment(OSL_PRECISION_MP, &c_mp, a_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i || c_sp_i != a_sp_i + 1) { error++; printf("Error osl_int_increment\n"); } } // osl_int_decrement if (!error) { osl_int_decrement(OSL_PRECISION_SP, &c_sp, a_sp); osl_int_decrement(OSL_PRECISION_DP, &c_dp, a_dp); osl_int_decrement(OSL_PRECISION_MP, &c_mp, a_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i || c_sp_i != a_sp_i - 1) { error++; printf("Error osl_int_decrement\n"); } } // osl_int_add if (!error) { osl_int_add(OSL_PRECISION_SP, &c_sp, a_sp, b_sp); osl_int_add(OSL_PRECISION_DP, &c_dp, a_dp, b_dp); osl_int_add(OSL_PRECISION_MP, &c_mp, a_mp, b_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i || c_sp_i != a_sp_i + b_sp_i) { error++; printf("Error osl_int_add\n"); } } // osl_int_add_si // osl_int_sub if (!error) { osl_int_sub(OSL_PRECISION_SP, &c_sp, a_sp, b_sp); osl_int_sub(OSL_PRECISION_DP, &c_dp, a_dp, b_dp); osl_int_sub(OSL_PRECISION_MP, &c_mp, a_mp, b_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i || c_sp_i != a_sp_i - b_sp_i) { error++; printf("Error osl_int_add\n"); } } // osl_int_mul if (!error) { osl_int_mul(OSL_PRECISION_SP, &c_sp, a_sp, b_sp); osl_int_mul(OSL_PRECISION_DP, &c_dp, a_dp, b_dp); osl_int_mul(OSL_PRECISION_MP, &c_mp, a_mp, b_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i || c_sp_i != a_sp_i * b_sp_i) { error++; printf("Error osl_int_mul\n"); } } // osl_int_mul_si if (!error) { osl_int_mul_si(OSL_PRECISION_SP, &c_sp, a_sp, b_sp_i); osl_int_mul_si(OSL_PRECISION_DP, &c_dp, a_dp, b_dp_i); osl_int_mul_si(OSL_PRECISION_MP, &c_mp, a_mp, b_mp_i); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i || c_sp_i != a_sp_i * b_sp_i) { error++; printf("Error osl_int_mul_si\n"); } } // osl_int_div_exact if (!error && b_sp_i != 0 && a_sp_i % b_sp_i == 0) { osl_int_div_exact(OSL_PRECISION_SP, &c_sp, a_sp, b_sp); osl_int_div_exact(OSL_PRECISION_DP, &c_dp, a_dp, b_dp); osl_int_div_exact(OSL_PRECISION_MP, &c_mp, a_mp, b_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i || c_sp_i != a_sp_i / b_sp_i) { error++; printf("Error osl_int_div_exact\n"); } } // osl_int_floor_div_q if (!error && b_sp_i != 0) { osl_int_floor_div_q(OSL_PRECISION_SP, &c_sp, a_sp, b_sp); osl_int_floor_div_q(OSL_PRECISION_DP, &c_dp, a_dp, b_dp); osl_int_floor_div_q(OSL_PRECISION_MP, &c_mp, a_mp, b_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i) { error++; printf("Error osl_int_floor_div_q\n"); } } // osl_int_floor_div_r if (!error && b_sp_i != 0) { osl_int_floor_div_r(OSL_PRECISION_SP, &c_sp, a_sp, b_sp); osl_int_floor_div_r(OSL_PRECISION_DP, &c_dp, a_dp, b_dp); osl_int_floor_div_r(OSL_PRECISION_MP, &c_mp, a_mp, b_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i) { error++; printf("Error osl_int_floor_div_r\n"); } } // osl_int_floor_div_q_r if (!error && b_sp_i != 0) { osl_int_t q_sp; osl_int_init(OSL_PRECISION_SP, &q_sp); osl_int_t q_dp; osl_int_init(OSL_PRECISION_DP, &q_dp); osl_int_t q_mp; osl_int_init(OSL_PRECISION_MP, &q_mp); osl_int_t r_sp; osl_int_init(OSL_PRECISION_SP, &r_sp); osl_int_t r_dp; osl_int_init(OSL_PRECISION_DP, &r_dp); osl_int_t r_mp; osl_int_init(OSL_PRECISION_MP, &r_mp); osl_int_floor_div_q_r(OSL_PRECISION_SP, &q_sp, &r_sp, a_sp, b_sp); osl_int_floor_div_q_r(OSL_PRECISION_DP, &q_dp, &r_dp, a_dp, b_dp); osl_int_floor_div_q_r(OSL_PRECISION_MP, &q_mp, &r_mp, a_mp, b_mp); int q_sp_i = osl_int_get_si(OSL_PRECISION_SP, q_sp); int q_dp_i = osl_int_get_si(OSL_PRECISION_DP, q_dp); int q_mp_i = osl_int_get_si(OSL_PRECISION_MP, q_mp); int r_sp_i = osl_int_get_si(OSL_PRECISION_SP, r_sp); int r_dp_i = osl_int_get_si(OSL_PRECISION_DP, r_dp); int r_mp_i = osl_int_get_si(OSL_PRECISION_MP, r_mp); if (q_sp_i != q_dp_i || q_sp_i != q_mp_i) { error++; printf("Error osl_int_floor_div_q_r\n"); } if (r_sp_i != r_dp_i || r_sp_i != r_mp_i) { error++; printf("Error osl_int_floor_div_q_r\n"); } } // osl_int_mod if (!error && b_sp_i != 0) { osl_int_mod(OSL_PRECISION_SP, &c_sp, a_sp, b_sp); osl_int_mod(OSL_PRECISION_DP, &c_dp, a_dp, b_dp); osl_int_mod(OSL_PRECISION_MP, &c_mp, a_mp, b_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i) { error++; printf("Error osl_int_mod\n"); } } // osl_int_gcd if (!error) { osl_int_gcd(OSL_PRECISION_SP, &c_sp, a_sp, b_sp); osl_int_gcd(OSL_PRECISION_DP, &c_dp, a_dp, b_dp); osl_int_gcd(OSL_PRECISION_MP, &c_mp, a_mp, b_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i) { error++; printf("Error osl_int_gcd\n"); } } // osl_int_oppose if (!error) { osl_int_oppose(OSL_PRECISION_SP, &c_sp, b_sp); osl_int_oppose(OSL_PRECISION_DP, &c_dp, b_dp); osl_int_oppose(OSL_PRECISION_MP, &c_mp, b_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i) { error++; printf("Error osl_int_oppose\n"); } } // osl_int_abs if (!error) { osl_int_abs(OSL_PRECISION_SP, &c_sp, b_sp); osl_int_abs(OSL_PRECISION_DP, &c_dp, b_dp); osl_int_abs(OSL_PRECISION_MP, &c_mp, b_mp); int c_sp_i = osl_int_get_si(OSL_PRECISION_SP, c_sp); int c_dp_i = osl_int_get_si(OSL_PRECISION_DP, c_dp); int c_mp_i = osl_int_get_si(OSL_PRECISION_MP, c_mp); if (c_sp_i != c_dp_i || c_sp_i != c_mp_i) { error++; printf("Error osl_int_abs\n"); } } // osl_int_size_in_base_2 if (!error) { size_t r_sp = osl_int_size_in_base_2(OSL_PRECISION_SP, b_sp); size_t r_dp = osl_int_size_in_base_2(OSL_PRECISION_DP, b_dp); size_t r_mp = osl_int_size_in_base_2(OSL_PRECISION_MP, b_mp); osl_int_set_si(OSL_PRECISION_SP, &c_sp, r_sp); osl_int_set_si(OSL_PRECISION_DP, &c_dp, r_dp); osl_int_set_si(OSL_PRECISION_MP, &c_mp, r_mp); if (r_sp != r_dp || r_sp != r_mp) { error++; printf("Error osl_int_size_in_base_2\n"); } } // osl_int_size_in_base_10 // if (!error) { // size_t r_sp = osl_int_size_in_base_10(OSL_PRECISION_SP, b_sp); // size_t r_dp = osl_int_size_in_base_10(OSL_PRECISION_DP, b_dp); // size_t r_mp = osl_int_size_in_base_10(OSL_PRECISION_MP, b_mp); // // osl_int_set_si(OSL_PRECISION_SP, &c_sp, r_sp); // osl_int_set_si(OSL_PRECISION_DP, &c_dp, r_dp); // osl_int_set_si(OSL_PRECISION_MP, &c_mp, r_mp); // // if (r_sp != r_dp || r_sp != r_mp) // { error++; printf("Error osl_int_size_in_base_10\n"); } // } // osl_int_eq // osl_int_ne // osl_int_pos // osl_int_neg // osl_int_zero // osl_int_one // osl_int_mone // osl_int_divisible if (error) { printf("Error with:\n"); printf("\n"); printf("a_sp = "); osl_int_print(stdout, OSL_PRECISION_SP, a_sp); printf("\n"); printf("a_dp = "); osl_int_print(stdout, OSL_PRECISION_DP, a_dp); printf("\n"); printf("a_mp = "); osl_int_print(stdout, OSL_PRECISION_MP, a_mp); printf("\n"); printf("\n"); printf("b_sp = "); osl_int_print(stdout, OSL_PRECISION_SP, b_sp); printf("\n"); printf("b_dp = "); osl_int_print(stdout, OSL_PRECISION_DP, b_dp); printf("\n"); printf("b_mp = "); osl_int_print(stdout, OSL_PRECISION_MP, b_mp); printf("\n"); printf("\n"); printf("c_sp = "); osl_int_print(stdout, OSL_PRECISION_SP, c_sp); printf("\n"); printf("c_dp = "); osl_int_print(stdout, OSL_PRECISION_DP, c_dp); printf("\n"); printf("c_mp = "); osl_int_print(stdout, OSL_PRECISION_MP, c_mp); printf("\n"); printf("\n"); nb_fail += error; } } } printf("%s ", argv[0]); printf("fails = %d\n", nb_fail); #else printf("%s ", argv[0]); printf("works only with GMP\n"); #endif return nb_fail; }
// assumes alpha-beta form is preserved, but makes no assumption on the // (in)equality order of appearance. int clay_relation_output_form(osl_relation_p relation) { clay_array_p equation_rows; int i, j, k, row_i, row_j; int nb_equations, nb_alpha_dims, nb_linearly_dependent_rows; int nb_steps; equation_rows = clay_array_malloc(); clay_relation_alpha_equation_rows(equation_rows, relation); nb_equations = equation_rows->size; nb_alpha_dims = (relation->nb_output_dims - 1) / 2; nb_linearly_dependent_rows = 0; nb_steps = nb_alpha_dims < nb_equations ? nb_alpha_dims : nb_equations; // Forward pass of integer Gauss elimination: make the alpha-part of the // relation matrix upper triangular by subtracting lines with coefficients. // Multiply both lines by the GCD to avoid having fractional coefficients, // then divide the remaining numbers by their GCD to avoid explosion. for (i = 0; i < nb_steps; i++) { for (j = i + 1; j < nb_equations; j++) { row_i = equation_rows->data[i]; row_j = equation_rows->data[j]; clay_relation_zero_coefficient(relation, row_i, row_j, 2*i + 2); } } // Linearly dependent rows should now contain zeros. for (i = 0; i < nb_equations; i++) { row_i = equation_rows->data[i]; if (clay_relation_line_is_zero(relation, row_i, 1, 1 + relation->nb_output_dims)) { nb_linearly_dependent_rows += 1; } } // Reverse pass of integer Gauss elimination: make the alpha-part of the // relation matrix diagonal. for (i = nb_steps - 1; i >= 0; i--) { for (j = i - 1; j >= 0; j--) { row_i = equation_rows->data[i]; row_j = equation_rows->data[j]; clay_relation_zero_coefficient(relation, row_i, row_j, 2*i + 2); } } // Flip diagonal elements if necessary. for (i = 0; i < nb_steps; i++) { row_i = equation_rows->data[i]; if (!osl_int_neg(relation->precision, relation->m[row_i][2*i + 2])) { for (k = 1; k < relation->nb_columns; k++) { if (k < 1 + relation->nb_output_dims && (k % 2) == 1) { continue; // ignore betas } osl_int_oppose(relation->precision, &relation->m[row_i][k], relation->m[row_i][k]); } } } if (nb_equations - nb_linearly_dependent_rows > nb_alpha_dims) { CLAY_debug("relation is defined by overdetermined matrix"); return -1; } else if (nb_equations - nb_linearly_dependent_rows < nb_alpha_dims) { // Relation is defined by underdetermined matrix, but this may be okay if // dimensions are defined implicitly. } // Check that diagonal elements are 1. They should be after gcd compression, // otherwise the corresponding output dimension is invalid (fractional). for (i = 0; i < nb_equations; i++) { row_i = equation_rows->data[i]; if (!osl_int_one(relation->precision, relation->m[row_i][2*i + 2])) { CLAY_debug("output form contains fractional dimension"); return -2; } } clay_array_free(equation_rows); return 0; }