Austin Schuh | dace2a6 | 2020-08-18 10:56:48 -0700 | [diff] [blame] | 1 | /* mpq_set_f -- set an mpq from an mpf. |
| 2 | |
| 3 | Copyright 2000-2002, 2018 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of the GNU MP Library. |
| 6 | |
| 7 | The GNU MP Library is free software; you can redistribute it and/or modify |
| 8 | it under the terms of either: |
| 9 | |
| 10 | * the GNU Lesser General Public License as published by the Free |
| 11 | Software Foundation; either version 3 of the License, or (at your |
| 12 | option) any later version. |
| 13 | |
| 14 | or |
| 15 | |
| 16 | * the GNU General Public License as published by the Free Software |
| 17 | Foundation; either version 2 of the License, or (at your option) any |
| 18 | later version. |
| 19 | |
| 20 | or both in parallel, as here. |
| 21 | |
| 22 | The GNU MP Library is distributed in the hope that it will be useful, but |
| 23 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 24 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 25 | for more details. |
| 26 | |
| 27 | You should have received copies of the GNU General Public License and the |
| 28 | GNU Lesser General Public License along with the GNU MP Library. If not, |
| 29 | see https://www.gnu.org/licenses/. */ |
| 30 | |
| 31 | #include "gmp-impl.h" |
| 32 | #include "longlong.h" |
| 33 | |
| 34 | |
| 35 | void |
| 36 | mpq_set_f (mpq_ptr q, mpf_srcptr f) |
| 37 | { |
| 38 | mp_size_t fexp = EXP(f); |
| 39 | mp_ptr fptr = PTR(f); |
| 40 | mp_size_t fsize = SIZ(f); |
| 41 | mp_size_t abs_fsize = ABS(fsize); |
| 42 | mp_limb_t flow; |
| 43 | |
| 44 | if (fsize == 0) |
| 45 | { |
| 46 | /* set q=0 */ |
| 47 | SIZ(NUM(q)) = 0; |
| 48 | SIZ(DEN(q)) = 1; |
| 49 | MPZ_NEWALLOC (DEN(q), 1)[0] = 1; |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | /* strip low zero limbs from f */ |
| 54 | flow = *fptr; |
| 55 | MPN_STRIP_LOW_ZEROS_NOT_ZERO (fptr, abs_fsize, flow); |
| 56 | |
| 57 | if (fexp >= abs_fsize) |
| 58 | { |
| 59 | /* radix point is to the right of the limbs, no denominator */ |
| 60 | mp_ptr num_ptr; |
| 61 | |
| 62 | num_ptr = MPZ_NEWALLOC (mpq_numref (q), fexp); |
| 63 | MPN_ZERO (num_ptr, fexp - abs_fsize); |
| 64 | MPN_COPY (num_ptr + fexp - abs_fsize, fptr, abs_fsize); |
| 65 | |
| 66 | SIZ(NUM(q)) = fsize >= 0 ? fexp : -fexp; |
| 67 | SIZ(DEN(q)) = 1; |
| 68 | MPZ_NEWALLOC (DEN(q), 1)[0] = 1; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | /* radix point is within or to the left of the limbs, use denominator */ |
| 73 | mp_ptr num_ptr, den_ptr; |
| 74 | mp_size_t den_size; |
| 75 | |
| 76 | den_size = abs_fsize - fexp; |
| 77 | num_ptr = MPZ_NEWALLOC (mpq_numref (q), abs_fsize); |
| 78 | den_ptr = MPZ_NEWALLOC (mpq_denref (q), den_size+1); |
| 79 | |
| 80 | if (flow & 1) |
| 81 | { |
| 82 | /* no powers of two to strip from numerator */ |
| 83 | |
| 84 | MPN_COPY (num_ptr, fptr, abs_fsize); |
| 85 | MPN_ZERO (den_ptr, den_size); |
| 86 | den_ptr[den_size] = 1; |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | /* right shift numerator, adjust denominator accordingly */ |
| 91 | int shift; |
| 92 | |
| 93 | den_size--; |
| 94 | count_trailing_zeros (shift, flow); |
| 95 | |
| 96 | mpn_rshift (num_ptr, fptr, abs_fsize, shift); |
| 97 | abs_fsize -= (num_ptr[abs_fsize-1] == 0); |
| 98 | |
| 99 | MPN_ZERO (den_ptr, den_size); |
| 100 | den_ptr[den_size] = GMP_LIMB_HIGHBIT >> (shift-1); |
| 101 | } |
| 102 | |
| 103 | SIZ(NUM(q)) = fsize >= 0 ? abs_fsize : -abs_fsize; |
| 104 | SIZ(DEN(q)) = den_size + 1; |
| 105 | } |
| 106 | } |