Debian Codegraph @

js/src/builtin/temporal/TemporalRoundingMode.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef builtin_temporal_TemporalRoundingMode_h
8 #define builtin_temporal_TemporalRoundingMode_h
9
10 #include "mozilla/Assertions.h"
11
12 #include <cmath>
13 #include <stdint.h>
14
15 #include "builtin/temporal/Crash.h"
16 #include "builtin/temporal/Int128.h"
17
18 namespace js::temporal {
19
20 // Overview of integer rounding modes is available at
21 // <https://en.wikipedia.org/wiki/Rounding#Rounding_to_integer>.
22 enum class TemporalRoundingMode {
23 // 1. Directed rounding to an integer.
24
25 // Round toward positive infinity.
26 Ceil,
27
28 // Round toward negative infinity.
29 Floor,
30
31 // Round toward infinity or round away from zero.
32 Expand,
33
34 // Round toward zero or round away from infinity.
35 Trunc,
36
37 // 2. Rounding to the nearest integer.
38
39 // Round half toward positive infinity.
40 HalfCeil,
41
42 // Round half toward negative infinity.
43 HalfFloor,
44
45 // Round half toward infinity or round half away from zero.
46 HalfExpand,
47
48 // Round half toward zero or round half away from infinity.
49 HalfTrunc,
50
51 // Round half to even.
52 HalfEven,
53 };
54
55 /**
56 * NegateRoundingMode ( roundingMode )
57 */
58 constexpr auto NegateRoundingMode(TemporalRoundingMode roundingMode) {
59 // Steps 1-5.
60 switch (roundingMode) {
61 case TemporalRoundingMode::Ceil:
62 return TemporalRoundingMode::Floor;
63
64 case TemporalRoundingMode::Floor:
65 return TemporalRoundingMode::Ceil;
66
67 case TemporalRoundingMode::HalfCeil:
68 return TemporalRoundingMode::HalfFloor;
69
70 case TemporalRoundingMode::HalfFloor:
71 return TemporalRoundingMode::HalfCeil;
72
73 case TemporalRoundingMode::Expand:
74 case TemporalRoundingMode::Trunc:
75 case TemporalRoundingMode::HalfExpand:
76 case TemporalRoundingMode::HalfTrunc:
77 case TemporalRoundingMode::HalfEven:
78 return roundingMode;
79 }
80 JS_CONSTEXPR_CRASH("invalid rounding mode");
81 }
82
83 /**
84 * Adjust the rounding mode to round negative values in the same direction as
85 * positive values.
86 */
87 constexpr auto ToPositiveRoundingMode(TemporalRoundingMode roundingMode) {
88 switch (roundingMode) {
89 case TemporalRoundingMode::Ceil:
90 case TemporalRoundingMode::Floor:
91 case TemporalRoundingMode::HalfCeil:
92 case TemporalRoundingMode::HalfFloor:
93 case TemporalRoundingMode::HalfEven:
94 // (Half-)Ceil/Floor round toward the same infinity for negative and
95 // positive values, so the rounding mode doesn't need to be adjusted. The
96 // same applies for half-even rounding.
97 return roundingMode;
98
99 case TemporalRoundingMode::Expand:
100 // Expand rounds positive values toward +infinity, but negative values
101 // toward -infinity. Adjust the rounding mode to Ceil to round negative
102 // values in the same direction as positive values.
103 return TemporalRoundingMode::Ceil;
104
105 case TemporalRoundingMode::Trunc:
106 // Truncation rounds positive values down toward zero, but negative values
107 // up toward zero. Adjust the rounding mode to Floor to round negative
108 // values in the same direction as positive values.
109 return TemporalRoundingMode::Floor;
110
111 case TemporalRoundingMode::HalfExpand:
112 // Adjust the rounding mode to Half-Ceil, similar to the Expand case.
113 return TemporalRoundingMode::HalfCeil;
114
115 case TemporalRoundingMode::HalfTrunc:
116 // Adjust the rounding mode to Half-Floor, similar to the Trunc case.
117 return TemporalRoundingMode::HalfFloor;
118 }
119 JS_CONSTEXPR_CRASH("unexpected rounding mode");
120 }
121
122 // Temporal performs division on "mathematical values" [1] with implies using
123 // infinite precision. This rules out using IEE-754 floating point types like
124 // `double`. It also means we can't implement the algorithms from the
125 // specification verbatim, but instead have to translate them into equivalent
126 // operations.
127 //
128 // Throughout the following division functions, the divisor is required to be
129 // positive. This allows to simplify the implementation, because it ensures
130 // non-zero quotient and remainder values have the same sign as the dividend.
131 //
132 // [1] https://tc39.es/ecma262/#mathematical-value
133
134 /**
135 * Compute ceiling division ⌈dividend / divisor⌉. The divisor must be a positive
136 * number.
137 */
138 constexpr int64_t CeilDiv(int64_t dividend, int64_t divisor) {
139 MOZ_ASSERT(divisor > 0, "negative divisor not supported");
140
141 // NB: Division and modulo operation are fused into a single machine code
142 // instruction by the compiler.
143 int64_t quotient = dividend / divisor;
144 int64_t remainder = dividend % divisor;
145
146 // Ceiling division rounds the quotient toward positive infinity. When the
147 // quotient is negative, this is equivalent to rounding toward zero. See [1].
148 //
149 // int64_t division truncates, so rounding toward zero for negative quotients
150 // is already covered. When there is a non-zero positive remainder, the
151 // quotient is positive and we have to increment it by one to implement
152 // rounding toward positive infinity.
153 //
154 // [1]
155 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
156 if (remainder > 0) {
157 quotient += 1;
158 }
159 return quotient;
160 }
161
162 /**
163 * Compute floor division ⌊dividend / divisor⌋. The divisor must be a positive
164 * number.
165 */
166 constexpr int64_t FloorDiv(int64_t dividend, int64_t divisor) {
167 MOZ_ASSERT(divisor > 0, "negative divisor not supported");
168
169 // NB: Division and modulo operation are fused into a single machine code
170 // instruction by the compiler.
171 int64_t quotient = dividend / divisor;
172 int64_t remainder = dividend % divisor;
173
174 // Floor division rounds the quotient toward negative infinity. When the
175 // quotient is positive, this is equivalent to rounding toward zero. See [1].
176 //
177 // int64_t division truncates, so rounding toward zero for positive quotients
178 // is already covered. When there is a non-zero negative remainder, the
179 // quotient is negative and we have to decrement it by one to implement
180 // rounding toward negative infinity.
181 //
182 // [1]
183 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
184 if (remainder < 0) {
185 quotient -= 1;
186 }
187 return quotient;
188 }
189
190 /**
191 * Compute "round toward infinity" division `dividend / divisor`. The divisor
192 * must be a positive number.
193 */
194 constexpr int64_t ExpandDiv(int64_t dividend, int64_t divisor) {
195 MOZ_ASSERT(divisor > 0, "negative divisor not supported");
196
197 // NB: Division and modulo operation are fused into a single machine code
198 // instruction by the compiler.
199 int64_t quotient = dividend / divisor;
200 int64_t remainder = dividend % divisor;
201
202 // "Round toward infinity" division rounds positive quotients toward positive
203 // infinity and negative quotients toward negative infinity. See [1].
204 //
205 // When there is a non-zero positive remainder, the quotient is positive and
206 // we have to increment it by one to implement rounding toward positive
207 // infinity. When there is a non-zero negative remainder, the quotient is
208 // negative and we have to decrement it by one to implement rounding toward
209 // negative infinity.
210 //
211 // [1]
212 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
213 if (remainder > 0) {
214 quotient += 1;
215 }
216 if (remainder < 0) {
217 quotient -= 1;
218 }
219 return quotient;
220 }
221
222 /**
223 * Compute truncating division `dividend / divisor`. The divisor must be a
224 * positive number.
225 */
226 constexpr int64_t TruncDiv(int64_t dividend, int64_t divisor) {
227 MOZ_ASSERT(divisor > 0, "negative divisor not supported");
228
229 // Truncating division rounds both positive and negative quotients toward
230 // zero, cf. [1].
231 //
232 // int64_t division truncates, so rounding toward zero implicitly happens.
233 //
234 // [1]
235 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
236 return dividend / divisor;
237 }
238
239 /**
240 * Compute "round half toward positive infinity" division `dividend / divisor`.
241 * The divisor must be a positive number.
242 */
243 inline int64_t HalfCeilDiv(int64_t dividend, int64_t divisor) {
244 MOZ_ASSERT(divisor > 0, "negative divisor not supported");
245
246 // NB: Division and modulo operation are fused into a single machine code
247 // instruction by the compiler.
248 int64_t quotient = dividend / divisor;
249 int64_t remainder = dividend % divisor;
250
251 // "Round half toward positive infinity" division rounds the quotient toward
252 // positive infinity when the fractional part of the remainder is ≥0.5. When
253 // the quotient is negative, this is equivalent to rounding toward zero
254 // instead of toward positive infinity. See [1].
255 //
256 // When the remainder is a non-zero positive value, the quotient is positive,
257 // too. When additionally the fractional part of the remainder is ≥0.5, we
258 // have to increment the quotient by one to implement rounding toward positive
259 // infinity.
260 //
261 // int64_t division truncates, so we implicitly round toward zero for negative
262 // quotients. When the absolute value of the fractional part of the remainder
263 // is >0.5, we should instead have rounded toward negative infinity, so we
264 // need to decrement the quotient by one.
265 //
266 // [1]
267 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
268 if (remainder > 0 && uint64_t(std::abs(remainder)) * 2 >= uint64_t(divisor)) {
269 quotient += 1;
270 }
271 if (remainder < 0 && uint64_t(std::abs(remainder)) * 2 > uint64_t(divisor)) {
272 quotient -= 1;
273 }
274 return quotient;
275 }
276
277 /**
278 * Compute "round half toward negative infinity" division `dividend / divisor`.
279 * The divisor must be a positive number.
280 */
281 inline int64_t HalfFloorDiv(int64_t dividend, int64_t divisor) {
282 MOZ_ASSERT(divisor > 0, "negative divisor not supported");
283
284 // NB: Division and modulo operation are fused into a single machine code
285 // instruction by the compiler.
286 int64_t quotient = dividend / divisor;
287 int64_t remainder = dividend % divisor;
288
289 // "Round half toward negative infinity" division rounds the quotient toward
290 // negative infinity when the fractional part of the remainder is ≥0.5. When
291 // the quotient is positive, this is equivalent to rounding toward zero
292 // instead of toward negative infinity. See [1].
293 //
294 // When the remainder is a non-zero negative value, the quotient is negative,
295 // too. When additionally the fractional part of the remainder is ≥0.5, we
296 // have to decrement the quotient by one to implement rounding toward negative
297 // infinity.
298 //
299 // int64_t division truncates, so we implicitly round toward zero for positive
300 // quotients. When the absolute value of the fractional part of the remainder
301 // is >0.5, we should instead have rounded toward positive infinity, so we
302 // need to increment the quotient by one.
303 //
304 // [1]
305 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
306 if (remainder < 0 && uint64_t(std::abs(remainder)) * 2 >= uint64_t(divisor)) {
307 quotient -= 1;
308 }
309 if (remainder > 0 && uint64_t(std::abs(remainder)) * 2 > uint64_t(divisor)) {
310 quotient += 1;
311 }
312 return quotient;
313 }
314
315 /**
316 * Compute "round half toward infinity" division `dividend / divisor`. The
317 * divisor must be a positive number.
318 */
319 inline int64_t HalfExpandDiv(int64_t dividend, int64_t divisor) {
320 MOZ_ASSERT(divisor > 0, "negative divisor not supported");
321
322 // NB: Division and modulo operation are fused into a single machine code
323 // instruction by the compiler.
324 int64_t quotient = dividend / divisor;
325 int64_t remainder = dividend % divisor;
326
327 // "Round half toward infinity" division rounds positive quotients whose
328 // remainder has a fractional part ≥0.5 toward positive infinity. And negative
329 // quotients whose remainder has a fractional part ≥0.5 toward negative
330 // infinity. See [1].
331 //
332 // int64_t division truncates, which means it rounds toward zero, so we have
333 // to increment resp. decrement the quotient when the fractional part of the
334 // remainder is ≥0.5 to round toward ±infinity.
335 //
336 // [1]
337 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
338 if (uint64_t(std::abs(remainder)) * 2 >= uint64_t(divisor)) {
339 quotient += (dividend > 0) ? 1 : -1;
340 }
341 return quotient;
342 }
343
344 /**
345 * Compute "round half toward zero" division `dividend / divisor`. The divisor
346 * must be a positive number.
347 */
348 inline int64_t HalfTruncDiv(int64_t dividend, int64_t divisor) {
349 MOZ_ASSERT(divisor > 0, "negative divisor not supported");
350
351 // NB: Division and modulo operation are fused into a single machine code
352 // instruction by the compiler.
353 int64_t quotient = dividend / divisor;
354 int64_t remainder = dividend % divisor;
355
356 // "Round half toward zero" division rounds both positive and negative
357 // quotients whose remainder has a fractional part ≤0.5 toward zero. See [1].
358 //
359 // int64_t division truncates, so we implicitly round toward zero. When the
360 // fractional part of the remainder is >0.5, we should instead have rounded
361 // toward ±infinity, so we need to increment resp. decrement the quotient by
362 // one.
363 //
364 // [1]
365 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
366 if (uint64_t(std::abs(remainder)) * 2 > uint64_t(divisor)) {
367 quotient += (dividend > 0) ? 1 : -1;
368 }
369 return quotient;
370 }
371
372 /**
373 * Compute "round half to even" division `dividend / divisor`. The divisor must
374 * be a positive number.
375 */
376 inline int64_t HalfEvenDiv(int64_t dividend, int64_t divisor) {
377 MOZ_ASSERT(divisor > 0, "negative divisor not supported");
378
379 // NB: Division and modulo operation are fused into a single machine code
380 // instruction by the compiler.
381 int64_t quotient = dividend / divisor;
382 int64_t remainder = dividend % divisor;
383
384 // "Round half to even" division rounds both positive and negative quotients
385 // to the nearest even integer. See [1].
386 //
387 // int64_t division truncates, so we implicitly round toward zero. When the
388 // fractional part of the remainder is 0.5 and the quotient is odd or when the
389 // fractional part of the remainder is >0.5, we should instead have rounded
390 // toward ±infinity, so we need to increment resp. decrement the quotient by
391 // one.
392 //
393 // [1]
394 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
395 if ((quotient & 1) == 1 &&
396 uint64_t(std::abs(remainder)) * 2 == uint64_t(divisor)) {
397 quotient += (dividend > 0) ? 1 : -1;
398 }
399 if (uint64_t(std::abs(remainder)) * 2 > uint64_t(divisor)) {
400 quotient += (dividend > 0) ? 1 : -1;
401 }
402 return quotient;
403 }
404
405 /**
406 * Perform `dividend / divisor` and round the result according to the given
407 * rounding mode.
408 */
409 inline int64_t Divide(int64_t dividend, int64_t divisor,
410 TemporalRoundingMode roundingMode) {
411 switch (roundingMode) {
412 case TemporalRoundingMode::Ceil:
413 return CeilDiv(dividend, divisor);
414 case TemporalRoundingMode::Floor:
415 return FloorDiv(dividend, divisor);
416 case TemporalRoundingMode::Expand:
417 return ExpandDiv(dividend, divisor);
418 case TemporalRoundingMode::Trunc:
419 return TruncDiv(dividend, divisor);
420 case TemporalRoundingMode::HalfCeil:
421 return HalfCeilDiv(dividend, divisor);
422 case TemporalRoundingMode::HalfFloor:
423 return HalfFloorDiv(dividend, divisor);
424 case TemporalRoundingMode::HalfExpand:
425 return HalfExpandDiv(dividend, divisor);
426 case TemporalRoundingMode::HalfTrunc:
427 return HalfTruncDiv(dividend, divisor);
428 case TemporalRoundingMode::HalfEven:
429 return HalfEvenDiv(dividend, divisor);
430 }
431 MOZ_CRASH("invalid rounding mode");
432 }
433
434 /**
435 * Compute ceiling division ⌈dividend / divisor⌉. The divisor must be a positive
436 * number.
437 */
438 inline Int128 CeilDiv(const Int128& dividend, const Int128& divisor) {
439 MOZ_ASSERT(divisor > Int128{0}, "negative divisor not supported");
440
441 auto [quotient, remainder] = dividend.divrem(divisor);
442
443 // Ceiling division rounds the quotient toward positive infinity. When the
444 // quotient is negative, this is equivalent to rounding toward zero. See [1].
445 //
446 // Int128 division truncates, so rounding toward zero for negative quotients
447 // is already covered. When there is a non-zero positive remainder, the
448 // quotient is positive and we have to increment it by one to implement
449 // rounding toward positive infinity.
450 //
451 // [1]
452 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
453 if (remainder > Int128{0}) {
454 quotient += Int128{1};
455 }
456 return quotient;
457 }
458
459 /**
460 * Compute floor division ⌊dividend / divisor⌋. The divisor must be a positive
461 * number.
462 */
463 inline Int128 FloorDiv(const Int128& dividend, const Int128& divisor) {
464 MOZ_ASSERT(divisor > Int128{0}, "negative divisor not supported");
465
466 auto [quotient, remainder] = dividend.divrem(divisor);
467
468 // Floor division rounds the quotient toward negative infinity. When the
469 // quotient is positive, this is equivalent to rounding toward zero. See [1].
470 //
471 // Int128 division truncates, so rounding toward zero for positive quotients
472 // is already covered. When there is a non-zero negative remainder, the
473 // quotient is negative and we have to decrement it by one to implement
474 // rounding toward negative infinity.
475 //
476 // [1]
477 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
478 if (remainder < Int128{0}) {
479 quotient -= Int128{1};
480 }
481 return quotient;
482 }
483
484 /**
485 * Compute "round toward infinity" division `dividend / divisor`. The divisor
486 * must be a positive number.
487 */
488 inline Int128 ExpandDiv(const Int128& dividend, const Int128& divisor) {
489 MOZ_ASSERT(divisor > Int128{0}, "negative divisor not supported");
490
491 auto [quotient, remainder] = dividend.divrem(divisor);
492
493 // "Round toward infinity" division rounds positive quotients toward positive
494 // infinity and negative quotients toward negative infinity. See [1].
495 //
496 // When there is a non-zero positive remainder, the quotient is positive and
497 // we have to increment it by one to implement rounding toward positive
498 // infinity. When there is a non-zero negative remainder, the quotient is
499 // negative and we have to decrement it by one to implement rounding toward
500 // negative infinity.
501 //
502 // [1]
503 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
504 if (remainder > Int128{0}) {
505 quotient += Int128{1};
506 }
507 if (remainder < Int128{0}) {
508 quotient -= Int128{1};
509 }
510 return quotient;
511 }
512
513 /**
514 * Compute truncating division `dividend / divisor`. The divisor must be a
515 * positive number.
516 */
517 inline Int128 TruncDiv(const Int128& dividend, const Int128& divisor) {
518 MOZ_ASSERT(divisor > Int128{0}, "negative divisor not supported");
519
520 // Truncating division rounds both positive and negative quotients toward
521 // zero, cf. [1].
522 //
523 // Int128 division truncates, so rounding toward zero implicitly happens.
524 //
525 // [1]
526 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
527 return dividend / divisor;
528 }
529
530 /**
531 * Compute "round half toward positive infinity" division `dividend / divisor`.
532 * The divisor must be a positive number.
533 */
534 inline Int128 HalfCeilDiv(const Int128& dividend, const Int128& divisor) {
535 MOZ_ASSERT(divisor > Int128{0}, "negative divisor not supported");
536
537 auto [quotient, remainder] = dividend.divrem(divisor);
538
539 // "Round half toward positive infinity" division rounds the quotient toward
540 // positive infinity when the fractional part of the remainder is ≥0.5. When
541 // the quotient is negative, this is equivalent to rounding toward zero
542 // instead of toward positive infinity. See [1].
543 //
544 // When the remainder is a non-zero positive value, the quotient is positive,
545 // too. When additionally the fractional part of the remainder is ≥0.5, we
546 // have to increment the quotient by one to implement rounding toward positive
547 // infinity.
548 //
549 // Int128 division truncates, so we implicitly round toward zero for negative
550 // quotients. When the absolute value of the fractional part of the remainder
551 // is >0.5, we should instead have rounded toward negative infinity, so we
552 // need to decrement the quotient by one.
553 //
554 // [1]
555 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
556 if (remainder > Int128{0} &&
557 Uint128(remainder.abs()) * Uint128{2} >= static_cast<Uint128>(divisor)) {
558 quotient += Int128{1};
559 }
560 if (remainder < Int128{0} &&
561 Uint128(remainder.abs()) * Uint128{2} > static_cast<Uint128>(divisor)) {
562 quotient -= Int128{1};
563 }
564 return quotient;
565 }
566
567 /**
568 * Compute "round half toward negative infinity" division `dividend / divisor`.
569 * The divisor must be a positive number.
570 */
571 inline Int128 HalfFloorDiv(const Int128& dividend, const Int128& divisor) {
572 MOZ_ASSERT(divisor > Int128{0}, "negative divisor not supported");
573
574 auto [quotient, remainder] = dividend.divrem(divisor);
575
576 // "Round half toward negative infinity" division rounds the quotient toward
577 // negative infinity when the fractional part of the remainder is ≥0.5. When
578 // the quotient is positive, this is equivalent to rounding toward zero
579 // instead of toward negative infinity. See [1].
580 //
581 // When the remainder is a non-zero negative value, the quotient is negative,
582 // too. When additionally the fractional part of the remainder is ≥0.5, we
583 // have to decrement the quotient by one to implement rounding toward negative
584 // infinity.
585 //
586 // Int128 division truncates, so we implicitly round toward zero for positive
587 // quotients. When the absolute value of the fractional part of the remainder
588 // is >0.5, we should instead have rounded toward positive infinity, so we
589 // need to increment the quotient by one.
590 //
591 // [1]
592 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
593 if (remainder < Int128{0} &&
594 Uint128(remainder.abs()) * Uint128{2} >= static_cast<Uint128>(divisor)) {
595 quotient -= Int128{1};
596 }
597 if (remainder > Int128{0} &&
598 Uint128(remainder.abs()) * Uint128{2} > static_cast<Uint128>(divisor)) {
599 quotient += Int128{1};
600 }
601 return quotient;
602 }
603
604 /**
605 * Compute "round half toward infinity" division `dividend / divisor`. The
606 * divisor must be a positive number.
607 */
608 inline Int128 HalfExpandDiv(const Int128& dividend, const Int128& divisor) {
609 MOZ_ASSERT(divisor > Int128{0}, "negative divisor not supported");
610
611 auto [quotient, remainder] = dividend.divrem(divisor);
612
613 // "Round half toward infinity" division rounds positive quotients whose
614 // remainder has a fractional part ≥0.5 toward positive infinity. And negative
615 // quotients whose remainder has a fractional part ≥0.5 toward negative
616 // infinity. See [1].
617 //
618 // Int128 division truncates, which means it rounds toward zero, so we have
619 // to increment resp. decrement the quotient when the fractional part of the
620 // remainder is ≥0.5 to round toward ±infinity.
621 //
622 // [1]
623 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
624 if (Uint128(remainder.abs()) * Uint128{2} >= static_cast<Uint128>(divisor)) {
625 quotient += (dividend > Int128{0}) ? Int128{1} : Int128{-1};
626 }
627 return quotient;
628 }
629
630 /**
631 * Compute "round half toward zero" division `dividend / divisor`. The divisor
632 * must be a positive number.
633 */
634 inline Int128 HalfTruncDiv(const Int128& dividend, const Int128& divisor) {
635 MOZ_ASSERT(divisor > Int128{0}, "negative divisor not supported");
636
637 auto [quotient, remainder] = dividend.divrem(divisor);
638
639 // "Round half toward zero" division rounds both positive and negative
640 // quotients whose remainder has a fractional part ≤0.5 toward zero. See [1].
641 //
642 // Int128 division truncates, so we implicitly round toward zero. When the
643 // fractional part of the remainder is >0.5, we should instead have rounded
644 // toward ±infinity, so we need to increment resp. decrement the quotient by
645 // one.
646 //
647 // [1]
648 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
649 if (Uint128(remainder.abs()) * Uint128{2} > static_cast<Uint128>(divisor)) {
650 quotient += (dividend > Int128{0}) ? Int128{1} : Int128{-1};
651 }
652 return quotient;
653 }
654
655 /**
656 * Compute "round half to even" division `dividend / divisor`. The divisor must
657 * be a positive number.
658 */
659 inline Int128 HalfEvenDiv(const Int128& dividend, const Int128& divisor) {
660 MOZ_ASSERT(divisor > Int128{0}, "negative divisor not supported");
661
662 auto [quotient, remainder] = dividend.divrem(divisor);
663
664 // "Round half to even" division rounds both positive and negative quotients
665 // to the nearest even integer. See [1].
666 //
667 // Int128 division truncates, so we implicitly round toward zero. When the
668 // fractional part of the remainder is 0.5 and the quotient is odd or when the
669 // fractional part of the remainder is >0.5, we should instead have rounded
670 // toward ±infinity, so we need to increment resp. decrement the quotient by
671 // one.
672 //
673 // [1]
674 // https://tc39.es/proposal-temporal/#table-temporal-unsigned-rounding-modes
675 if ((quotient & Int128{1}) == Int128{1} &&
676 Uint128(remainder.abs()) * Uint128{2} == static_cast<Uint128>(divisor)) {
677 quotient += (dividend > Int128{0}) ? Int128{1} : Int128{-1};
678 }
679 if (Uint128(remainder.abs()) * Uint128{2} > static_cast<Uint128>(divisor)) {
680 quotient += (dividend > Int128{0}) ? Int128{1} : Int128{-1};
681 }
682 return quotient;
683 }
684
685 /**
686 * Perform `dividend / divisor` and round the result according to the given
687 * rounding mode.
688 */
689 inline Int128 Divide(const Int128& dividend, const Int128& divisor,
690 TemporalRoundingMode roundingMode) {
691 switch (roundingMode) {
692 case TemporalRoundingMode::Ceil:
693 return CeilDiv(dividend, divisor);
694 case TemporalRoundingMode::Floor:
695 return FloorDiv(dividend, divisor);
696 case TemporalRoundingMode::Expand:
697 return ExpandDiv(dividend, divisor);
698 case TemporalRoundingMode::Trunc:
699 return TruncDiv(dividend, divisor);
700 case TemporalRoundingMode::HalfCeil:
701 return HalfCeilDiv(dividend, divisor);
702 case TemporalRoundingMode::HalfFloor:
703 return HalfFloorDiv(dividend, divisor);
704 case TemporalRoundingMode::HalfExpand:
705 return HalfExpandDiv(dividend, divisor);
706 case TemporalRoundingMode::HalfTrunc:
707 return HalfTruncDiv(dividend, divisor);
708 case TemporalRoundingMode::HalfEven:
709 return HalfEvenDiv(dividend, divisor);
710 }
711 MOZ_CRASH("invalid rounding mode");
712 }
713
714 } /* namespace js::temporal */
715
716 #endif /* builtin_temporal_TemporalRoundingMode_h */