Skip to content

Commit 7c517bf

Browse files
committed
[libc][stdfix] Implement fxdivi functions
Signed-off-by: Shreeyash Pandey <[email protected]>
1 parent 8bf105c commit 7c517bf

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
986986
libc.src.stdfix.idivulr
987987
libc.src.stdfix.idivuk
988988
libc.src.stdfix.idivulk
989+
libc.src.stdfix.rdivi
989990
)
990991
endif()
991992

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
10191019
libc.src.stdfix.idivulr
10201020
libc.src.stdfix.idivuk
10211021
libc.src.stdfix.idivulk
1022+
libc.src.stdfix.rdivi
10221023
)
10231024
endif()
10241025

libc/include/stdfix.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,11 @@ functions:
544544
arguments:
545545
- type: unsigned long accum
546546
guard: LIBC_COMPILER_HAS_FIXED_POINT
547+
- name: rdivi
548+
standards:
549+
- stdc_ext
550+
return_type: fract
551+
arguments:
552+
- type: int
553+
- type: int
554+
guard: LIBC_COMPILER_HAS_FIXED_POINT

libc/src/__support/fixed_point/fx_bits.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,47 @@ idiv(T x, T y) {
224224
return static_cast<XType>(result);
225225
}
226226

227+
inline long accum nrstep(long accum d, long accum x0) {
228+
auto v = x0 * (2 - (d * x0));
229+
return v;
230+
}
231+
232+
/* Divide the two integers and return a fixed_point value
233+
*
234+
* For reference, see:
235+
* https://en.wikipedia.org/wiki/Division_algorithm#Newton%E2%80%93Raphson_division
236+
* https://stackoverflow.com/a/9231996
237+
*/
238+
template <typename XType> LIBC_INLINE constexpr XType divi(int n, int d) {
239+
// If the value of the second operand of the / operator is zero, the
240+
// behavior is undefined. Ref: ISO/IEC TR 18037:2008(E) p.g. 16
241+
LIBC_CRASH_ON_VALUE(d, 0);
242+
243+
unsigned int nv = static_cast<unsigned int>(n);
244+
unsigned int dv = static_cast<unsigned int>(d);
245+
unsigned int clz = cpp::countl_zero<unsigned int>(dv) - 1;
246+
unsigned long int scaled_val = dv << clz;
247+
/* Scale denominator to be in the range of [0.5,1] */
248+
FXBits<long accum> d_scaled{scaled_val};
249+
unsigned long int scaled_val_n = nv << clz;
250+
/* Scale the numerator as much as the denominator to maintain correctness of
251+
* the original equation
252+
*/
253+
FXBits<long accum> n_scaled{scaled_val_n};
254+
long accum n_scaled_val = n_scaled.get_val();
255+
long accum d_scaled_val = d_scaled.get_val();
256+
/* x0 = (48/17) - (32/17) * d_n */
257+
long accum a = 2.8235lk; /* 48/17 */
258+
long accum b = 1.8823lk; /* 32/17 */
259+
long accum initial_approx = a - (b * d_scaled_val);
260+
long accum val = nrstep(d_scaled_val, initial_approx);
261+
val = nrstep(d_scaled_val, val);
262+
val = nrstep(d_scaled_val, val);
263+
val = nrstep(d_scaled_val, val);
264+
long accum res = n_scaled_val * val;
265+
return static_cast<XType>(res);
266+
}
267+
227268
} // namespace fixed_point
228269
} // namespace LIBC_NAMESPACE_DECL
229270

libc/src/stdfix/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ foreach(suffix IN ITEMS r lr k lk ur ulr uk ulk)
8989
)
9090
endforeach()
9191

92+
foreach(suffix IN ITEMS r)
93+
add_entrypoint_object(
94+
${suffix}divi
95+
HDRS
96+
${suffix}divi.h
97+
SRCS
98+
${suffix}divi.cpp
99+
COMPILE_OPTIONS
100+
${libc_opt_high_flag}
101+
DEPENDS
102+
libc.src.__support.fixed_point.fx_bits
103+
)
104+
endforeach()
105+
92106
add_entrypoint_object(
93107
uhksqrtus
94108
HDRS

libc/test/src/stdfix/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ foreach(suffix IN ITEMS r lr k lk ur ulr uk ulk)
120120
)
121121
endforeach()
122122

123+
foreach(suffix IN ITEMS r)
124+
add_libc_test(
125+
${suffix}divi_test
126+
SUITE
127+
libc-stdfix-tests
128+
HDRS
129+
DivITest.h
130+
SRCS
131+
${suffix}divi_test.cpp
132+
DEPENDS
133+
libc.src.stdfix.${suffix}divi
134+
libc.src.__support.fixed_point.fx_bits
135+
libc.hdr.signal_macros
136+
)
137+
endforeach()
138+
123139
add_libc_test(
124140
uhksqrtus_test
125141
SUITE

0 commit comments

Comments
 (0)