|
1 | | -/* |
2 | | - * Copyright (c) 2018 Jorge Aparicio |
3 | | - * |
4 | | - * Permission is hereby granted, free of charge, to any |
5 | | - * person obtaining a copy of this software and associated |
6 | | - * documentation files (the "Software"), to deal in the |
7 | | - * Software without restriction, including without |
8 | | - * limitation the rights to use, copy, modify, merge, |
9 | | - * publish, distribute, sublicense, and/or sell copies of |
10 | | - * the Software, and to permit persons to whom the Software |
11 | | - * is furnished to do so, subject to the following |
12 | | - * conditions: |
13 | | - * |
14 | | - * The above copyright notice and this permission notice |
15 | | - * shall be included in all copies or substantial portions |
16 | | - * of the Software. |
17 | | - * |
18 | | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
19 | | - * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
20 | | - * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
21 | | - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
22 | | - * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
23 | | - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
24 | | - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
25 | | - * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
26 | | - * DEALINGS IN THE SOFTWARE. |
27 | | - */ |
28 | | - |
29 | | -use core::f32; |
30 | | -use core::u32; |
31 | | - |
32 | | -#[no_mangle] |
33 | | -pub fn fmodf(x: f32, y: f32) -> f32 { |
34 | | - let mut uxi = x.to_bits(); |
35 | | - let mut uyi = y.to_bits(); |
36 | | - let mut ex = (uxi >> 23 & 0xff) as i32; |
37 | | - let mut ey = (uyi >> 23 & 0xff) as i32; |
38 | | - let sx = uxi & 0x80000000; |
39 | | - let mut i; |
40 | | - |
41 | | - if uyi << 1 == 0 || y.is_nan() || ex == 0xff { |
42 | | - return (x * y) / (x * y); |
43 | | - } |
44 | | - |
45 | | - if uxi << 1 <= uyi << 1 { |
46 | | - if uxi << 1 == uyi << 1 { |
47 | | - return 0.0 * x; |
48 | | - } |
49 | | - |
50 | | - return x; |
51 | | - } |
52 | | - |
53 | | - /* normalize x and y */ |
54 | | - if ex == 0 { |
55 | | - i = uxi << 9; |
56 | | - while i >> 31 == 0 { |
57 | | - ex -= 1; |
58 | | - i <<= 1; |
59 | | - } |
60 | | - |
61 | | - uxi <<= -ex + 1; |
62 | | - } else { |
63 | | - uxi &= u32::MAX >> 9; |
64 | | - uxi |= 1 << 23; |
65 | | - } |
66 | | - |
67 | | - if ey == 0 { |
68 | | - i = uyi << 9; |
69 | | - while i >> 31 == 0 { |
70 | | - ey -= 1; |
71 | | - i <<= 1; |
72 | | - } |
73 | | - |
74 | | - uyi <<= -ey + 1; |
75 | | - } else { |
76 | | - uyi &= u32::MAX >> 9; |
77 | | - uyi |= 1 << 23; |
78 | | - } |
79 | | - |
80 | | - /* x mod y */ |
81 | | - while ex > ey { |
82 | | - i = uxi - uyi; |
83 | | - if i >> 31 == 0 { |
84 | | - if i == 0 { |
85 | | - return 0.0 * x; |
86 | | - } |
87 | | - uxi = i; |
88 | | - } |
89 | | - uxi <<= 1; |
90 | | - |
91 | | - ex -= 1; |
92 | | - } |
93 | | - |
94 | | - i = uxi - uyi; |
95 | | - if i >> 31 == 0 { |
96 | | - if i == 0 { |
97 | | - return 0.0 * x; |
98 | | - } |
99 | | - uxi = i; |
100 | | - } |
101 | | - |
102 | | - while uxi >> 23 == 0 { |
103 | | - uxi <<= 1; |
104 | | - ex -= 1; |
105 | | - } |
106 | | - |
107 | | - /* scale result up */ |
108 | | - if ex > 0 { |
109 | | - uxi -= 1 << 23; |
110 | | - uxi |= (ex as u32) << 23; |
111 | | - } else { |
112 | | - uxi >>= -ex + 1; |
113 | | - } |
114 | | - uxi |= sx; |
115 | | - |
116 | | - f32::from_bits(uxi) |
117 | | -} |
118 | | - |
119 | 1 | /* origin: FreeBSD /usr/src/lib/msun/src/e_sqrtf.c */ |
120 | 2 | /* |
121 | 3 | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. |
|
0 commit comments