Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created January 5, 2020 12:12
Show Gist options
  • Save untodesu/003ca3dbdf4be1606f981f1eb316b12c to your computer and use it in GitHub Desktop.
Save untodesu/003ca3dbdf4be1606f981f1eb316b12c to your computer and use it in GitHub Desktop.
math.h 1/sqrt(double) -- Q_rsqrt(double)
-nan --- -nan
inf --- inf
1.000000 --- 1.000000
0.707107 --- 0.707107
0.577350 --- 0.577350
0.500000 --- 0.500000
0.447214 --- 0.447214
0.408248 --- 0.408248
0.377964 --- 0.377964
0.353553 --- 0.353553
0.333333 --- 0.333333
0.316228 --- 0.316228
0.301511 --- 0.301511
0.288675 --- 0.288675
0.277350 --- 0.277350
0.267261 --- 0.267261
0.258199 --- 0.258199
0.250000 --- 0.250000
0.242536 --- 0.242536
0.235702 --- 0.235702
0.229416 --- 0.229416
0.223607 --- 0.223607
0.218218 --- 0.218218
0.213201 --- 0.213201
0.208514 --- 0.208514
0.204124 --- 0.204124
0.200000 --- 0.200000
0.196116 --- 0.196116
0.192450 --- 0.192450
0.188982 --- 0.188982
0.185695 --- 0.185695
0.182574 --- 0.182574
0.179605 --- 0.179605
0.176777 --- 0.176777
0.174078 --- 0.174078
0.171499 --- 0.171499
0.169031 --- 0.169031
0.166667 --- 0.166667
0.164399 --- 0.164399
0.162221 --- 0.162221
0.160128 --- 0.160128
0.158114 --- 0.158114
0.156174 --- 0.156174
0.154303 --- 0.154303
0.152499 --- 0.152499
0.150756 --- 0.150756
0.149071 --- 0.149071
0.147442 --- 0.147442
0.145865 --- 0.145865
0.144338 --- 0.144338
0.142857 --- 0.142857
0.141421 --- 0.141421
0.140028 --- 0.140028
0.138675 --- 0.138675
0.137361 --- 0.137361
0.136083 --- 0.136083
0.134840 --- 0.134840
0.133631 --- 0.133631
0.132453 --- 0.132453
0.131306 --- 0.131306
0.130189 --- 0.130189
0.129099 --- 0.129099
0.128037 --- 0.128037
0.127000 --- 0.127000
0.125988 --- 0.125988
0.125000 --- 0.125000
0.124035 --- 0.124035
0.123091 --- 0.123091
0.122169 --- 0.122169
0.121268 --- 0.121268
0.120386 --- 0.120386
0.119523 --- 0.119523
0.118678 --- 0.118678
0.117851 --- 0.117851
0.117041 --- 0.117041
0.116248 --- 0.116248
0.115470 --- 0.115470
0.114708 --- 0.114708
0.113961 --- 0.113961
0.113228 --- 0.113228
0.112509 --- 0.112509
0.111803 --- 0.111803
0.111111 --- 0.111111
0.110432 --- 0.110432
0.109764 --- 0.109764
0.109109 --- 0.109109
0.108465 --- 0.108465
0.107833 --- 0.107833
0.107211 --- 0.107211
0.106600 --- 0.106600
0.106000 --- 0.106000
0.105409 --- 0.105409
0.104828 --- 0.104828
0.104257 --- 0.104257
0.103695 --- 0.103695
0.103142 --- 0.103142
0.102598 --- 0.102598
0.102062 --- 0.102062
0.101535 --- 0.101535
0.101015 --- 0.101015
0.100504 --- 0.100504
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#define C_INF (double)(1e+300 * 1e+300)
#define C_NAN (double)(C_INF * 0.0)
double Q_rsqrt(double num)
{
// Number should be positive non-zero
if(num <= 0.0) {
return (num ? C_NAN : C_INF);
}
const double xhalf = (num * 0.5);
int64_t i = *((int64_t *)&num);
i = (0x5FE6EC85E7DE30DA - (i >> 1));
num = *((double *)&i);
// lets get more precise value
num *= (1.5 - (xhalf * num * num));
num *= (1.5 - (xhalf * num * num)); // can be removed
num *= (1.5 - (xhalf * num * num)); // equals to (1.0/sqrt), also can be removed
return num;
}
int main(void)
{
printf("1/sqrt(double) -- Q_rsqrt(double)\n");
for(double i = -1.0; i < 100.0; i++) {
printf("%f --- %f\n", 1.0/sqrt(i), Q_rsqrt(i));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment