Implement better hypot function

This commit is contained in:
Justine Tunney 2021-02-21 11:06:18 -08:00
parent 6c7b8facaa
commit 4a5698b5c9
14 changed files with 293 additions and 62 deletions

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
@ -16,15 +16,16 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
#include "libc/math.h"
// Returns euclidean distance in 2d space.
hypotf: .leafprologue
.profilable
mulss %xmm1,%xmm1
mulss %xmm0,%xmm0
addss %xmm1,%xmm0
sqrtss %xmm0,%xmm0
.leafepilogue
.endfn hypotf,globl
/**
* Returns euclidean distance.
*/
double hypot(double a, double b) {
double r;
if (isinf(a) || isinf(b)) return INFINITY;
if (isunordered(a, b)) return NAN;
if (!a) return 0;
r = b / a;
return fabs(a) * sqrt(1 + r * r);
}

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
@ -16,15 +16,16 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
#include "libc/math.h"
// Returns euclidean distance in 2d space.
hypot: .leafprologue
.profilable
mulsd %xmm1,%xmm1
mulsd %xmm0,%xmm0
addsd %xmm1,%xmm0
sqrtsd %xmm0,%xmm0
.leafepilogue
.endfn hypot,globl
/**
* Returns euclidean distance.
*/
float hypotf(float a, float b) {
float r;
if (isinf(a) || isinf(b)) return INFINITY;
if (isunordered(a, b)) return NAN;
if (!a) return 0;
r = b / a;
return fabsf(a) * sqrtf(1 + r * r);
}

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
@ -16,19 +16,16 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
#include "libc/math.h"
// Returns euclidean distance in 2d space.
hypotl: push %rbp
mov %rsp,%rbp
.profilable
fldt 32(%rbp)
fldt 16(%rbp)
fmul %st,%st
fxch
fmul %st,%st
faddp
pop %rbp
ret
.endfn hypotl,globl
/**
* Returns euclidean distance.
*/
long double hypotl(long double a, long double b) {
long double r;
if (isinf(a) || isinf(b)) return INFINITY;
if (isunordered(a, b)) return NAN;
if (!a) return 0;
r = b / a;
return fabsl(a) * sqrtl(1 + r * r);
}