File: complex.ha

package info (click to toggle)
hare 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,948 kB
  • sloc: asm: 1,264; makefile: 123; sh: 114; lisp: 101
file content (660 lines) | stat: -rw-r--r-- 21,042 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

// Sections of the code below are based on Go's implementation, which is, in
// turn, based on Cephes Math Library. The original C code can be found at
// http://netlib.sandia.gov/cephes/c9x-complex/.
//
// Cephes Math Library Release 2.8:  June, 2000
// Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
//
// The readme file at http://netlib.sandia.gov/cephes/ says:
//    Some software in this archive may be from the book _Methods and
// Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster
// International, 1989) or from the Cephes Mathematical Library, a
// commercial product. In either event, it is copyrighted by the author.
// What you see here may be used freely but it comes with no support or
// guarantee.
//
//   The two known misprints in the book are repaired here in the
// source listings for the gamma function and the incomplete beta
// integral.
//
//   Stephen L. Moshier
//   moshier@na-net.ornl.gov
//
// The Go copyright notice:
// ====================================================
// Copyright (c) 2009 The Go Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ====================================================

use math;
use math::checked;

// A complex number containing a real component and an imaginary component,
// represented as two single-precision floating point numbers.
export type c64 = (f32, f32);

// A complex number containing a real component and an imaginary component,
// represented as two double-precision floating point numbers.
export type c128 = (f64, f64);

// A tagged union of all complex types.
export type complex = (c64 | c128);

// Converts a [[c64]] to a [[c128]].
export fn c64to128(z: c64) c128 = (z.0: f64, z.1: f64);

// Truncates a [[c128]] to a [[c64]]. Precision may be lost.
export fn c128to64(z: c128) c64 = (z.0: f32, z.1: f32);

// Adds two complex numbers
export fn addc64(a: c64, b: c64) c64 = (a.0 + b.0, a.1 + b.1);

// Adds two complex numbers.
export fn addc128(a: c128, b: c128) c128 = (a.0 + b.0, a.1 + b.1);

// Subtracts two complex numbers.
export fn subc64(a: c64, b: c64) c64 = (a.0 - b.0, a.1 - b.1);

// Subtracts two complex numbers.
export fn subc128(a: c128, b: c128) c128 = (a.0 - b.0, a.1 - b.1);

// Multiplies two complex numbers.
export fn mulc64(a: c64, b: c64) c64 =
	(a.0 * b.0 - a.1 * b.1, a.1 * b.0 + a.0 * b.1);

// Multiplies two complex numbers.
export fn mulc128(a: c128, b: c128) c128 =
	(a.0 * b.0 - a.1 * b.1, a.1 * b.0 + a.0 * b.1);

// Divides two complex numbers.
export fn divc64(a: c64, b: c64) c64 = {
	const denom = b.0 * b.0 + b.1 * b.1;
	return (
		(a.0 * b.0 + a.1 * b.1) / denom,
		(a.1 * b.0 - a.0 * b.1) / denom,
	);
};

// Divides two complex numbers.
export fn divc128(a: c128, b: c128) c128 = {
	const denom = b.0 * b.0 + b.1 * b.1;
	return (
		(a.0 * b.0 + a.1 * b.1) / denom,
		(a.1 * b.0 - a.0 * b.1) / denom,
	);
};

// Takes the conjugate of a complex number by negating the imaginary component.
export fn conjc64(z: c64) c64 = (z.0, -z.1);

// Takes the conjugate of a complex number by negating the imaginary component.
export fn conjc128(z: c128) c128 = (z.0, -z.1);

// Takes the absolute value of a complex number.
export fn absc128(z: c128) f64 = math::hypotf64(z.0, z.1);

// Gets the argument, or phase, of a complex number.
export fn argc128(z: c128) f64 = math::atan2f64(z.1, z.0);

// Checks if two complex numbers are equal. Be sure to take floating point
// round-off errors into account.
export fn equalc64(a: c64, b: c64) bool = a.0 == b.0 && a.1 == b.1;

// Checks if two complex numbers are equal. Be sure to take floating point
// round-off errors into account.
export fn equalc128(a: c128, b: c128) bool = a.0 == b.0 && a.1 == b.1;

// Checks if two complex numbers are equal. Be sure to take floating point
// round-off errors into account.
export fn equal(a: complex, b: complex) bool = {
	match (a) {
	case let a: c64 =>
		return equalc64(a, b as c64);
	case let a: c128 =>
		return equalc128(a, b as c128);
	};
};

// Returns [[math::E]] raised to the power of z.
export fn expc128(z: c128) c128 = {
	if (math::isinf(z.0)) {
		if (z.0 > 0f64 && z.1 == 0f64) {
			return z;
		};
		if (math::isinf(z.1) || math::isnan(z.1)) {
			if (z.0 < 0f64) {
				return (0f64, math::copysignf64(0f64, z.1));
			} else {
				return (math::INF, math::NAN);
			};
		};
	} else if (math::isnan(z.0) && z.1 == 0f64) {
		return (math::NAN, z.1);
	};
	return rectc128(math::expf64(z.0), z.1);
};

// Returns true if the given complex number is infinite.
export fn isinf(z: c128) bool = math::isinf(z.0) || math::isinf(z.1);

// Returns true if the given complex number is NaN -- that is -- either
// component is NaN and neither component is an infinity.
export fn isnan(z: c128) bool =
	!isinf(z) && (math::isnan(z.0) || math::isnan(z.1));

// Returns the natural logarithm of z.
export fn logc128(z: c128) c128 = (math::logf64(absc128(z)), argc128(z));

// Negates z.
export fn negc64(z: c64) c64 = (-z.0, -z.1);

// Negates z.
export fn negc128(z: c128) c128 = (-z.0, -z.1);

// Creates a new [[c128]] from the polar coordinates (r, theta).
export fn rectc128(r: f64, theta: f64) c128 =
	(r * math::cosf64(theta), r * math::sinf64(theta));

// Returns the polar coordinates of z.
export fn polarc128(z: c128) (f64, f64) = (absc128(z), argc128(z));

// Returns a raised to the power of b.
export fn powc128(a: c128, b: c128) c128 = {
	if (a.0 == 0f64 && a.1 == 0f64) {
		if (isnan(b)) {
			return (math::NAN, math::NAN);
		} else if (b.0 == 0f64) {
			return (1f64, 0f64);
		} else if (b.0 < 0f64) {
			return (math::INF, if (b.1 == 0f64) 0f64 else math::INF);
		} else {
			assert(b.0 > 0f64);
			return (0f64, 0f64);
		};
	};
	const mod = absc128(a);
	if (mod == 0f64) {
		return (0f64, 0f64);
	};
	let r = math::powf64(mod, b.0);
	const phase = argc128(a);
	let theta = b.0 * phase;
	if (b.1 != 0f64) {
		r *= math::expf64(-b.1 * phase);
		theta += b.1 * math::logf64(mod);
	};
	return rectc128(r, theta);
};

// Projects z onto the surface of a Riemann Sphere. If z is finite, it projects
// to itself. If z is infinite, it projects to positive infinity on the real
// axis.
export fn projc64(z: c64) c64 =
	if (!isinf(c64to128(z))) z else (math::INF, math::copysignf32(0f32, z.1));

// Projects z onto the surface of a Riemann Sphere. If z is finite, it projects
// to itself. If z is infinite, it projects to positive infinity on the real
// axis.
export fn projc128(z: c128) c128 =
	if (!isinf(z)) z else (math::INF, math::copysignf64(0f64, z.1));

// Returns the square root of z.
export fn sqrtc128(z: c128) c128 = {
	if (z.1 == 0f64) {
		if (z.0 == 0f64) {
			return (0f64, z.1);
		};
		if (z.0 < 0f64) {
			return (0f64, math::copysignf64(math::sqrtf64(-z.0), z.1));
		};
		return (math::sqrtf64(z.0), z.1);
	};
	if (math::isinf(z.1)) {
		return (math::INF, z.1);
	};
	if (z.0 == 0f64) {
		if (z.1 < 0f64) {
			const r = math::sqrtf64(-0.5 * z.1);
			return (r, -r);
		} else {
			const r = math::sqrtf64(0.5 * z.1);
			return (r, r);
		};
	};
	let a = z.0, b = z.1;
	const scale = if (math::absf64(a) > 4f64 || math::absf64(b) > 4f64) {
		a *= 0.25;
		b *= 0.25;
		yield 2f64;
	} else {
		a *= 1.8014398509481984e16; // 2**54
		b *= 1.8014398509481984e16;
		yield 7.450580596923828125e-9; // 2**-27
	};
	let r = math::hypotf64(a, b);
	const t = if (a > 0f64) {
		const t = math::sqrtf64(0.5 * r + 0.5 * a);
		r = scale * math::absf64(0.5 * b / t);
		yield t * scale;
	} else {
		r = math::sqrtf64(0.5 * r - 0.5 * a);
		const t = scale * math::absf64(0.5 * b / r);
		r *= scale;
		yield t;
	};
	return (t, if (b < 0f64) -r else r);
};

// Returns the sine of z, in radians.
export fn sinc128(z: c128) c128 = {
	if (z.1 == 0f64 && (math::isinf(z.0) || math::isnan(z.0))) {
		return (math::NAN, z.1);
	} else if (math::isinf(z.1)) {
		if (z.0 == 0f64) {
			return z;
		} else if (math::isinf(z.0) || math::isnan(z.0)) {
			return (math::NAN, z.1);
		};
	} else if (z.0 == 0f64 && math::isnan(z.1)) {
		return z;
	};
	const shch = sinhcosh(z.1);
	return (math::sinf64(z.0) * shch.1, math::cosf64(z.0) * shch.0);
};

// Returns the hyperbolic sine of z.
export fn sinhc128(z: c128) c128 = {
	if (z.0 == 0f64 && (math::isinf(z.1) || math::isnan(z.1))) {
		return (z.0, math::NAN);
	} else if (math::isinf(z.0)) {
		if (z.1 == 0f64) {
			return z;
		} else if (math::isinf(z.1) || math::isnan(z.1)) {
			return (z.0, math::NAN);
		};
	} else if (z.1 == 0f64 && math::isnan(z.0)) {
		return (math::NAN, z.1);
	};
	const shch = sinhcosh(z.0);
	return (math::cosf64(z.1) * shch.0, math::sinf64(z.1) * shch.1);
};

// Returns the arcsine, in radians, of z.
export fn asinc128(z: c128) c128 = {
	if (z.1 == 0f64 && math::absf64(z.0) <= 1f64) {
		return (math::asinf64(z.0), z.1);
	} else if (z.0 == 0f64 && math::absf64(z.1) <= 1f64) {
		return (z.0, math::asinhf64(z.1));
	} else if (math::isnan(z.1)) {
		if (z.0 == 0f64) {
			return (z.0, math::NAN);
		} else if (math::isinf(z.0)) {
			return (math::NAN, z.0);
		} else {
			return (math::NAN, math::NAN);
		};
	} else if (math::isinf(z.1)) {
		if (math::isnan(z.0)) {
			return z;
		} else if (math::isinf(z.0)) {
			return (math::copysignf64(math::PI / 4f64, z.0), z.1);
		} else {
			return (math::copysignf64(0f64, z.0), z.1);
		};
	} else if (math::isinf(z.0)) {
		return (math::copysignf64(math::PI / 2f64, z.0),
			math::copysignf64(z.0, z.1));
	};
	const ct = (-z.1, z.0); // i * z
	const zz = mulc128(z, z);
	const z1 = (1f64 - zz.0, -zz.1); // 1 - z * z
	const z2 = sqrtc128(z1); // z2 = sqrt(1 - z * z)
	const w = logc128(addc128(ct, z2));
	return (w.1, -w.0); // -i * w
};

// Returns the inverse hyperbolic sine of z.
export fn asinhc128(z: c128) c128 = {
	if (z.1 == 0f64 && math::absf64(z.0) <= 1f64) {
		return (math::asinhf64(z.0), z.1);
	} else if (z.0 == 0f64 && math::absf64(z.1) <= 1f64) {
		return (z.0, math::asinf64(z.1));
	} else if (math::isinf(z.0)) {
		if (math::isinf(z.1)) {
			return (z.0, math::copysignf64(math::PI / 4f64, z.1));
		} else if (math::isnan(z.1)) {
			return z;
		} else {
			return (z.0, math::copysignf64(0f64, z.1));
		};
	} else if (math::isnan(z.0)) {
		if (z.1 == 0f64) {
			return z;
		} else if (math::isinf(z.1)) {
			return (z.1, z.0);
		} else {
			return (math::NAN, math::NAN);
		};
	} else if (math::isinf(z.1)) {
		return (math::copysignf64(z.1, z.0),
			math::copysignf64(math::PI / 2f64, z.1));
	};
	const zz = mulc128(z, z);
	const z1 = (1f64 + zz.0, zz.1); // 1 + z * z
	return logc128(addc128(z, sqrtc128(z1))); // log(x + sqrt(1 + x * x))
};

// Returns the cosine of z, in radians.
export fn cosc128(z: c128) c128 = {
	if (z.1 == 0f64 && (math::isinf(z.0) || math::isnan(z.0))) {
		return (math::NAN, -z.1 * math::copysignf64(0f64, z.0));
	} else if (math::isinf(z.1)) {
		if (z.0 == 0f64) {
			return (math::INF, -z.0 * math::copysignf64(0f64, z.1));
		} else if (math::isinf(z.0) || math::isnan(z.0)) {
			return (math::INF, math::NAN);
		};
	} else if (z.0 == 0f64 && math::isnan(z.1)) {
		return (math::NAN, 0f64);
	};
	const shch = sinhcosh(z.1);
	return (math::cosf64(z.0) * shch.1, -math::sinf64(z.0) * shch.0);
};

// Returns the hyperbolic cosine of z.
export fn coshc128(z: c128) c128 = {
	if (z.0 == 0f64 && (math::isinf(z.1) || math::isnan(z.1))) {
		return (math::NAN, z.0 * math::copysignf64(0f64, z.1));
	} else if (math::isinf(z.0)) {
		if (z.1 == 0f64) {
			return (math::INF, z.1 * math::copysignf64(0f64, z.0));
		} else if (math::isinf(z.1) || math::isnan(z.1)) {
			return (math::INF, math::NAN);
		};
	} else if (z.1 == 0f64 && math::isnan(z.0)) {
		return (math::NAN, z.1);
	};
	const shch = sinhcosh(z.0);
	return (math::cosf64(z.1) * shch.1, math::sinf64(z.1) * shch.0);
};

// Returns the arccosine, in radians, of z.
export fn acosc128(z: c128) c128 = {
	const w = asinc128(z);
	return (math::PI / 2f64 - w.0, -w.1);
};

// Returns the inverse hyperbolic cosine of z.
export fn acoshc128(z: c128) c128 = {
	if (z.0 == 0f64 && z.1 == 0f64) {
		return (0f64, math::copysignf64(math::PI / 2f64, z.1));
	};
	const w = acosc128(z);
	if (w.1 <= 0f64) {
		return (-w.1, w.0); // i * w
	};
	return (w.1, -w.0); // -i * w
};

fn sinhcosh(x: f64) (f64, f64) = {
	if (math::absf64(x) <= 0.5) {
		return (math::sinhf64(x), math::coshf64(x));
	};
	let e = math::expf64(x);
	const ei = 0.5 / e;
	e *= 0.5;
	return (e - ei, e + ei);
};

// reducePi reduces the input argument x to the range (-Pi/2, Pi/2].
// x must be greater than or equal to 0. For small arguments it
// uses Cody-Waite reduction in 3 f64 parts based on:
// "Elementary Function Evaluation: Algorithms and Implementation"
// Jean-Michel Muller, 1997.
// For very large arguments it uses Payne-Hanek range reduction based on:
// "ARGUMENT REDUCTION FOR HUGE ARGUMENTS: Good to the Last Bit"
// K. C. Ng et al, March 24, 1992.
fn reducePi(x: f64) f64 = {
	// reduceThreshold is the maximum value of x where the reduction using
	// Cody-Waite reduction still gives accurate results. This threshold
	// is set by t*PIn being representable as a f64 without error
	// where t is given by t = floor(x * (1 / Pi)) and PIn are the leading partial
	// terms of Pi. Since the leading terms, PI1 and PI2 below, have 30 and 32
	// trailing zero bits respectively, t should have less than 30 significant bits.
	//	t < 1<<30  -> floor(x*(1/Pi)+0.5) < 1<<30 -> x < (1<<30-1) * Pi - 0.5
	// So, conservatively we can take x < 1<<30.
	const reduceThreshold = (1u64 << 30): f64;
	if (math::absf64(x) < reduceThreshold) {
		// Use Cody-Waite reduction in three parts.
		// PI1, PI2 and PI3 comprise an extended precision value of PI
		// such that PI ~= PI1 + PI2 + PI3. The parts are chosen so
		// that PI1 and PI2 have an approximately equal number of trailing
		// zero bits. This ensures that t*PI1 and t*PI2 are exact for
		// large integer values of t. The full precision PI3 ensures the
		// approximation of PI is accurate to 102 bits to handle cancellation
		// during subtraction.
		const PI1 = 3.141592502593994;      // 0x400921fb40000000
		const PI2 = 1.5099578831723193e-07; // 0x3e84442d00000000
		const PI3 = 1.0780605716316238e-14; // 0x3d08469898cc5170
		let t = x / math::PI;
		t += 0.5;
		t = (t: i64): f64;
		return ((x - t*PI1) - t*PI2) - t*PI3;
	};
	// Must apply Payne-Hanek range reduction
	const mask: u64 = 0x7FF;
	const shift: u64 = 64 - 11 - 1;
	const bias: u64 = 1023;
	const fracMask: u64 = (1u64 << shift) - 1;

	// Extract out the integer and exponent such that,
	// x = ix * 2 ** exp.
	let ix: u64 = math::f64bits(x);
	let exp: u64 = (ix >> shift & mask) - bias - shift;
	ix &= fracMask;
	ix |= 1u64 << shift;

	// mPi is the binary digits of 1/Pi as a u64 array,
	// that is, 1/Pi = Sum mPi[i]*2^(-64*i).
	// 19 64-bit digits give 1216 bits of precision
	// to handle the largest possible float64 exponent.
	let mPi: [_]u64 = [
		0x0000000000000000,
		0x517cc1b727220a94,
		0xfe13abe8fa9a6ee0,
		0x6db14acc9e21c820,
		0xff28b1d5ef5de2b0,
		0xdb92371d2126e970,
		0x0324977504e8c90e,
		0x7f0ef58e5894d39f,
		0x74411afa975da242,
		0x74ce38135a2fbf20,
		0x9cc8eb1cc1a99cfa,
		0x4e422fc5defc941d,
		0x8ffc4bffef02cc07,
		0xf79788c5ad05368f,
		0xb69b3f6793e584db,
		0xa7a31fb34f2ff516,
		0xba93dd63f5f2f8bd,
		0x9e839cfbc5294975,
		0x35fdafd88fc6ae84,
		0x2b0198237e3db5d5,
	];
	// Use the exponent to extract the 3 appropriate u64 digits from mPi,
	// B ~ (z0, z1, z2), such that the product leading digit has the exponent -64.
	// Note, exp >= 50 since x >= reduceThreshold and exp < 971 for maximum f64.
	let digit: u64 = (exp + 64): u64 / 64;
	let bitshift: u64 = (exp + 64): u64 % 64;
	let z0: u64 = (mPi[digit] << bitshift) | (mPi[digit+1] >> (64 - bitshift));
	let z1: u64 = (mPi[digit+1] << bitshift) | (mPi[digit+2] >> (64 - bitshift));
	let z2: u64 = (mPi[digit+2] << bitshift) | (mPi[digit+3] >> (64 - bitshift));
	// Multiply mantissa by the digits and extract the upper two digits (hi, lo).
	let (z2hi, z2lo) = math::mulu64(z2, ix);
	let (z1hi, z1lo) = math::mulu64(z1, ix);
	let z0lo: u64 = z0 * ix;
	let (lo, c) = checked::addu64(z1lo, z2hi);
	let hi = z0lo + z1hi + (if (c) 1u64 else 0u64);
	// Find the magnitude of the fraction.
	let lz: u8 = math::leading_zeros_u64(hi);
	let e: u64 = (bias - (lz + 1)): u64;
	// Clear implicit mantissa bit and shift into place.
	hi = (hi << (lz + 1)) | (lo >> (64 - (lz + 1)));
	hi >>= 64 - shift;
	// Include the exponent and convert to a float.
	hi |= e << shift;
	x = math::f64frombits(hi);
	// map to (-Pi/2, Pi/2]
	if (x > 0.5) {
		x -= 1f64;
	};
	return math::PI * x;
};

// Taylor series expansion for cosh(2y) - cos(2x)
fn tanSeries(z: c128) f64 = {
	const MACHEP = 1f64/(1u64 << 53): f64;
	let x = math::absf64(2f64 * z.0);
	let y = math::absf64(2f64 * z.1);
	x = reducePi(x);
	x = x * x;
	y = y * y;

	let x2 = 1f64;
	let y2 = 1f64;
	let f = 1f64;
	let rn = 0f64;
	let d = 0f64;

	for (true) {
		rn += 1f64;
		f *= rn;
		rn += 1f64;
		f *= rn;
		x2 *= x;
		y2 *= y;
		let t = y2 + x2;
		t /= f;
		d += t;

		rn += 1f64;
		f *= rn;
		rn += 1f64;
		f *= rn;
		x2 *= x;
		y2 *= y;
		t = y2 - x2;
		t /= f;
		d += t;
		if (!(math::absf64(t/d) > MACHEP)) {
			// Caution: Use ! and > instead of <= for correct behavior if t/d is NaN.
			// See issue https://github.com/golang/go/issues/17577.
			break;
		};
	};
	return d;
};

// Returns the tangent of x.
export fn tanc128(x: c128) c128 = {
	if (math::isinf(x.1)) {
		if (math::isinf(x.0) || math::isnan(x.0)) {
			return (math::copysignf64(0f64, x.0), math::copysignf64(1f64, x.1));
		};
		return (math::copysignf64(0f64, math::sinf64(2f64*x.0)), math::copysignf64(1f64, x.1));
	};
	if (x.0 == 0f64 && math::isnan(x.1)) {
		return x;
	};
	let d = math::cosf64(2f64*x.0) + math::coshf64(2f64*x.1);
	if (math::absf64(d) < 0.25f64) {
		d = tanSeries(x);
	};
	if (d == 0f64) {
		return (math::INF, math::INF);
	};
	return (math::sinf64(2f64*x.0)/d, math::sinhf64(2f64*x.1)/d);
};

// Returns the hyperbolic tangent of x.
export fn tanhc128(x: c128) c128 = {
	if (math::isinf(x.0)) {
		if (math::isinf(x.1) || math::isnan(x.1)) {
			return (math::copysignf64(1f64, x.0), math::copysignf64(0f64, x.1));
		};
		return (math::copysignf64(1f64, x.0), math::copysignf64(0f64, math::sinf64(2f64*x.1)));
	};
	if (x.1 == 0f64 && math::isnan(x.0)) {
		return x;
	};
	let d = math::coshf64(2f64*x.0) + math::cosf64(2f64*x.1);
	if (d == 0f64) {
		return (math::INF, math::INF);
	};
	return (math::sinhf64(2f64*x.0)/d, math::sinf64(2f64*x.1)/d);
};

// Returns the inverse tangent of x.
export fn atanc128(x: c128) c128 = {
	if (x.1 == 0f64) {
		return (math::atanf64(x.0), x.1);
	} else if (x.0 == 0f64 && math::absf64(x.1) <= 1f64) {
		return (x.0, math::atanhf64(x.1));
	} else if (math::isinf(x.1) || math::isinf(x.0)) {
		if (math::isnan(x.0)) {
			return (math::NAN, math::copysignf64(0f64, x.1));
		};
		return (math::copysignf64(math::PI/2f64, x.0), math::copysignf64(0f64, x.1));
	} else if (math::isnan(x.0) || math::isnan(x.1)) {
		return (math::NAN, math::NAN);
	};
	let x2 = x.0 * x.0;
	let a = 1f64 - x2 - x.1 * x.1;
	if (a == 0f64) {
		return (math::NAN, math::NAN);
	};
	let t = 0.5f64 * math::atan2f64(2f64*x.0, a);
	let w = reducePi(t);
	t = x.1 - 1f64;
	let b = x2 + t*t;
	if (b == 0f64) {
		return (math::NAN, math::NAN);
	};
	t = x.1 + 1f64;
	let c = (x2 + t*t) / b;
	return (w, 0.25f64*math::logf64(c));
};

// Returns the inverse hyperbolic tangent of x.
export fn atanhc128(x: c128) c128 = {
	let z = (-x.1, x.0); // z = i * x
	z = atanc128(z);
	return (z.1, -z.0); // z = -i * z
};