Austin Schuh | bb1338c | 2024-06-15 19:31:16 -0700 | [diff] [blame] | 1 | /* mpz_set_f (dest_integer, src_float) -- Assign DEST_INTEGER from SRC_FLOAT. |
| 2 | |
| 3 | Copyright 1996, 2001, 2012, 2016 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 | |
| 33 | |
| 34 | void |
| 35 | mpz_set_f (mpz_ptr w, mpf_srcptr u) |
| 36 | { |
| 37 | mp_ptr wp, up; |
| 38 | mp_size_t size; |
| 39 | mp_exp_t exp; |
| 40 | |
| 41 | /* abs(u)<1 truncates to zero */ |
| 42 | exp = EXP (u); |
| 43 | if (exp <= 0) |
| 44 | { |
| 45 | SIZ(w) = 0; |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | wp = MPZ_NEWALLOC (w, exp); |
| 50 | up = PTR(u); |
| 51 | |
| 52 | size = SIZ (u); |
| 53 | SIZ(w) = (size >= 0 ? exp : -exp); |
| 54 | size = ABS (size); |
| 55 | |
| 56 | if (exp > size) |
| 57 | { |
| 58 | /* pad with low zeros to get a total "exp" many limbs */ |
| 59 | mp_size_t zeros = exp - size; |
| 60 | MPN_ZERO (wp, zeros); |
| 61 | wp += zeros; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | /* exp<=size, truncate to the high "exp" many limbs */ |
| 66 | up += (size - exp); |
| 67 | size = exp; |
| 68 | } |
| 69 | |
| 70 | MPN_COPY (wp, up, size); |
| 71 | } |