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
//! Famous "Fast inverse square root" algorithm implementation
//! along with obligatory original comments.

#![cfg_attr(all(feature = "nightly",test), feature(asm,test,core_intrinsics))]

#[cfg(all(feature = "nightly",test))]
extern crate test;

#[cfg(test)]
#[macro_use] extern crate more_asserts;

use std::mem;
use std::f32;
use std::f64;

/// A trait that allows calculation of inverse square root into a Float(32).
pub trait InvSqrt32 {
	/// Calculates the inverse square root of a number
	fn inv_sqrt32(self) -> f32;
}

/// A trait that allows calculation of inverse square root into a Float(64).
pub trait InvSqrt64 {
	/// Calculates the inverse square root of a number.
	fn inv_sqrt64(self) -> f64;
}

/// Implementation of InvSqrt32 using the "Fast inverse square root" method.
/// This implementation somewhat provides look'n'feel of the original one.
/// If you want to uncomment 2nd iteration you actually have to make y mutable.
#[allow(non_upper_case_globals)]
impl InvSqrt32 for f32 {
	fn inv_sqrt32(self: f32) -> f32 {
		if cfg!(not(feature = "omit-checking")) {
			if self.signum() != 1.0 {
				return f32::NAN;
			} else if self == f32::INFINITY {
				return 0.0;
			} else if self < f32::MIN_POSITIVE {
				return f32::INFINITY;
			}
		}

		// Magic number based on Chris Lomont work:
		// const MAGIC_U32: u32 = 0x5f375a86;
		// The Original Magic Number:
		// const MAGIC_32: u32 = 0x5f3759df;
		const threehalfs: f32 = 1.5f32;
		let x2: f32 = self * 0.5f32;
		let mut i: u32 = unsafe { mem::transmute(self) }; // evil floating point bit level hacking
		i = 0x5f375a86 - (i >> 1);                        // what the fuck?
		let y: f32 = unsafe { mem::transmute(i) };
		let y  = y * ( threehalfs - ( x2 * y * y ) );     // 1st iteration
//		y  = y * ( threehalfs - ( x2 * y * y ) );       // 2nd iteration, this can be removed

		return y;
	}
}

/// Implementation of InvSqrt64 using the "Fast inverse square root" method.
/// This implementation provides somewhat more Rusty look'n'feel.
impl InvSqrt64 for f64 {
	fn inv_sqrt64(self: f64) -> f64 {
		if cfg!(not(feature = "omit-checking")) {
			if self.signum() != 1.0 {
				return f64::NAN;
			} else if self == f64::INFINITY {
				return 0.0;
			} else if self < f64::MIN_POSITIVE {
				return f64::INFINITY;
			}
		}

		// Magic number based on Chris Lomont work:
		const MAGIC_U64: u64 = 0x5fe6ec85e7de30da;
		const THREEHALFS: f64 = 1.5;
		let x2 = self * 0.5;
		let i = MAGIC_U64 - ( unsafe { mem::transmute::<_, u64>(self) } >> 1);
		let y: f64 = unsafe { mem::transmute(i) };

		y * ( THREEHALFS - ( x2 * y * y ) )
	}
}

impl InvSqrt32 for f64 {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt32 for i8 {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt32 for u8 {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt32 for i16 {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt32 for u16 {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt32 for i32 {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt32 for u32 {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt32 for i64 {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt32 for u64 {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt32 for isize {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt32 for usize {
	fn inv_sqrt32(self) -> f32 {
		(self as f32).inv_sqrt32()
	}
}

impl InvSqrt64 for f32 {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

impl InvSqrt64 for i8 {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

impl InvSqrt64 for u8 {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

impl InvSqrt64 for i16 {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

impl InvSqrt64 for u16 {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

impl InvSqrt64 for i32 {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

impl InvSqrt64 for u32 {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

impl InvSqrt64 for i64 {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

impl InvSqrt64 for u64 {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

impl InvSqrt64 for isize {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

impl InvSqrt64 for usize {
	fn inv_sqrt64(self) -> f64 {
		(self as f64).inv_sqrt64()
	}
}

#[cfg(test)]
mod test32 {
	use std::f32;
	use super::InvSqrt32;

	#[cfg(feature = "nightly")]
	use test::{black_box,Bencher};

	#[cfg(all(feature = "nightly", any(target_arch = "x86", target_arch = "x86_64")))]
	mod asm_x86 {
		use test::{black_box,Bencher};

		#[inline(always)]
		fn rsqrtss_mem(f: f32) -> f32 {
			let ret;
			unsafe {
				asm!("rsqrtss $1, $0" : "=x"(ret) : "m"(f) );
			}

			ret
		}

		#[inline(always)]
		fn rsqrtss_reg(f: f32) -> f32 {
			let ret;
			unsafe {
				asm!("rsqrtss $1, $0" : "=x"(ret) : "x"(f) );
			}

			ret
		}

		#[test]
		fn test_rsqrtss_mem() {
			let value: f32 = 11.1111;
			let result = rsqrtss_mem(value);
			assert_le!((0.3 - result).abs(), 0.0005);
		}

		#[test]
		fn test_rsqrtss_reg() {
			let value: f32 = 11.1111;
			let result = rsqrtss_reg(value);
			assert_le!((0.3 - result).abs(), 0.0005);
		}

		#[bench]
		fn bench_plain_rsqrtss_mem(b: &mut Bencher) {
			b.iter(|| {
				let f = black_box(1.2345f32);
				rsqrtss_mem(f)
			});
		}

		#[bench]
		fn bench_plain_rsqrtss_reg(b: &mut Bencher) {
			b.iter(|| {
				let f = black_box(1.2345f32);
				rsqrtss_reg(f)
			});
		}

		#[bench]
		fn bench_real_rsqrtss_reg_real(b: &mut Bencher) {
			b.iter(|| {
				let f = black_box(1.2345f32);
				let v = black_box(2.3456f32);
				v * rsqrtss_reg(f)
			});
		}
	}

	#[cfg(feature = "nightly")]
	mod llvm {
		use test::{black_box,Bencher};

		#[inline(always)]
		fn llvm_sqrt32(f: f32) -> f32 {
			unsafe { ::std::intrinsics::sqrtf32(f) }
		}

		#[inline(always)]
		fn llvm_inv_sqrt32(f: f32) -> f32 {
			1.0 / llvm_sqrt32(f)
		}

		#[test]
		fn test_llvm() {
			let value: f32 = 11.1111;
			let result = llvm_inv_sqrt32(value);
			assert_le!((0.3 - result).abs(), 0.0005);
		}

		#[bench]
		fn bench_plain_llvm(b: &mut Bencher) {
			b.iter(|| {
				let f = black_box(1.2345f32);
				llvm_inv_sqrt32(f)
			});
		}

		#[bench]
		fn bench_real_llvm(b: &mut Bencher) {
			b.iter(|| {
				let f = black_box(1.2345f32);
				let v = black_box(2.3456f32);
				v / llvm_sqrt32(f)
			});
		}
	}

	#[inline(always)]
	fn ref_inv_sqrt32(f: f32) -> f32 {
		1.0f32 / f.sqrt()
	}

	fn relative_difference(lhs: f32, rhs: f32) -> f32 {
		return 2.0 * (rhs - lhs).abs() / (rhs.abs() + lhs.abs());
	}

	#[test]
	fn test_f32() {
		let value: f32 = 11.1111;
		assert_le!((0.3 - value.inv_sqrt32()).abs(), 0.0005);

		let mut value: f32 = 0.00000001f32;
		while value < 100.4f32 {
			let result = value.inv_sqrt32();
			let ref_result = ref_inv_sqrt32(value);
			let relative_diff = relative_difference(result, ref_result);
			//println!("inv_sqrt32() -> {}: {} / {} [{}]", value, result, ref_result, relative_diff);
			assert_le!(relative_diff, 0.002);
			value += 0.01f32;
		}
	}

	#[test]
	fn test_zero() {
		let zero = 0.0f32;
		assert_eq!(zero.inv_sqrt32(), f32::INFINITY);
	}

	#[test]
	fn test_negative() {
		let negative = -1.0f32;
		assert!(negative.inv_sqrt32().is_nan());
	}

	#[test]
	fn test_negative_zero() {
		let negative_zero = -0.0f32;
		assert!(negative_zero.inv_sqrt32().is_nan());
	}

	#[test]
	fn test_i8() {
		let value: i8 = 55;
		assert_le!((0.1348399725 - value.inv_sqrt32()).abs(), 0.00001);
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_plain_ref(b: &mut Bencher) {
		b.iter(|| {
			let f = black_box(1.2345f32);
			ref_inv_sqrt32(f)
		});
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_recip_sqrt(b: &mut Bencher) {
		b.iter(|| {
			let f = black_box(1.2345f32);
			f.recip().sqrt()
		});
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_sqrt_recip(b: &mut Bencher) {
		b.iter(|| {
			let f = black_box(1.2345f32);
			f.sqrt().recip()
		});
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_plain_impl(b: &mut Bencher) {
		b.iter(|| {
			let f = black_box(1.2345f32);
			f.inv_sqrt32()
		});
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_real_ref(b: &mut Bencher) {
		b.iter(|| {
			let v = black_box(2.3456f32);
			let f = black_box(1.2345f32);
			v / f.sqrt()
		});
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_real_impl(b: &mut Bencher) {
		b.iter(|| {
			let v = black_box(2.3456f32);
			let f = black_box(1.2345f32);
			v * f.inv_sqrt32()
		});
	}
}

#[cfg(test)]
mod test64 {
	use std::f64;
	use super::InvSqrt64;

	#[cfg(feature = "nightly")]
	use test::{black_box,Bencher};

	#[inline(always)]
	fn ref_inv_sqrt64(f: f64) -> f64 {
		1.0f64/f.sqrt()
	}

	fn relative_difference(lhs: f64, rhs: f64) -> f64 {
		return 2.0 * (rhs - lhs).abs() / (rhs.abs() + lhs.abs());
	}

	#[test]
	fn test_f64() {
		let value: f64 = 11.1111;
		assert_le!((0.3 - value.inv_sqrt64()).abs(), 0.0005);

		let mut value: f64 = 0.00000001f64;
		while value < 100.4f64 {
			let result = value.inv_sqrt64();
			let ref_result = ref_inv_sqrt64(value);
			let relative_diff = relative_difference(result, ref_result);
			//println!("inv_sqrt64(): {} -> {} / {} [{}]", value, result, ref_result, relative_diff);
			assert_le!(relative_diff, 0.002);
			value += 0.01f64;
		}
	}

	#[test]
	fn test_zero() {
		let zero = 0.0f64;
		assert_eq!(zero.inv_sqrt64(), f64::INFINITY);
	}

	#[test]
	fn test_negative() {
		let negative = -1.0f64;
		assert!(negative.inv_sqrt64().is_nan());
	}

	#[test]
	fn test_negative_zero() {
		let negative_zero = -0.0f64;
		assert!(negative_zero.inv_sqrt64().is_nan());
	}

	#[test]
	fn test_i8() {
		let value: i8 = 55;
		assert_le!((0.1348399725 - value.inv_sqrt64()).abs(), 0.00001);
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_plain_ref(b: &mut Bencher) {
		b.iter(|| {
			let f = black_box(1.2345f64);
			ref_inv_sqrt64(f)
		});
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_recip_sqrt(b: &mut Bencher) {
		b.iter(|| {
			let f = black_box(1.2345f64);
			f.recip().sqrt()
		});
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_sqrt_recip(b: &mut Bencher) {
		b.iter(|| {
			let f = black_box(1.2345f64);
			f.sqrt().recip()
		});
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_plain_impl(b: &mut Bencher) {
		b.iter(|| {
			let f = black_box(1.2345f64);
			f.inv_sqrt64()
		});
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_real_ref(b: &mut Bencher) {
		b.iter(|| {
			let v = black_box(2.3456f64);
			let f = black_box(1.2345f64);
			v / f.sqrt()
		});
	}

	#[cfg(feature = "nightly")]
	#[bench]
	fn bench_real_impl(b: &mut Bencher) {
		b.iter(|| {
			let v = black_box(2.3456f64);
			let f = black_box(1.2345f64);
			v * f.inv_sqrt64()
		});
	}
}