/*-*- 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 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         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/math.h"
#include "libc/mem/gc.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#include "libc/x/xasprintf.h"

double _atanh(double) asm("atanh");
float _atanhf(float) asm("atanhf");
long double _atanhl(long double) asm("atanhl");

TEST(atanh, test) {
  EXPECT_STREQ("0", _gc(xdtoa(_atanh(0))));
  EXPECT_STREQ("-0", _gc(xdtoa(_atanh(-0.))));
  EXPECT_STREQ(".549306144334055", _gc(xdtoa(_atanh(.5))));
  EXPECT_STREQ("-.549306144334055", _gc(xdtoa(_atanh(-.5))));
  EXPECT_STREQ("INFINITY", _gc(xdtoa(_atanh(+1))));
  EXPECT_STREQ("-INFINITY", _gc(xdtoa(_atanh(-1))));
  EXPECT_TRUE(isnan(_atanh(+1.1)));
  EXPECT_TRUE(isnan(_atanh(-1.1)));
  EXPECT_STREQ("-2.1073424255447e-08",
               _gc(xasprintf("%.15g", _atanh(-2.1073424255447e-08))));
}

TEST(atanhl, test) {
  EXPECT_STREQ("0", _gc(xdtoal(_atanhl(0))));
  EXPECT_STREQ("-0", _gc(xdtoal(_atanhl(-0.))));
  EXPECT_STREQ(".5493061443340548", _gc(xdtoal(_atanhl(.5))));
  EXPECT_STREQ("-.5493061443340548", _gc(xdtoal(_atanhl(-.5))));
  EXPECT_STREQ("INFINITY", _gc(xdtoal(_atanhl(+1))));
  EXPECT_STREQ("-INFINITY", _gc(xdtoal(_atanhl(-1))));
  EXPECT_TRUE(isnan(_atanhl(+1.1)));
  EXPECT_TRUE(isnan(_atanhl(-1.1)));
}

TEST(atanhf, test) {
  EXPECT_STREQ("0", _gc(xdtoaf(_atanhf(0))));
  EXPECT_STREQ("-0", _gc(xdtoaf(_atanhf(-0.))));
  EXPECT_STREQ(".549306", _gc(xdtoaf(_atanhf(.5))));
  EXPECT_STREQ("-.549306", _gc(xdtoaf(_atanhf(-.5))));
  EXPECT_STREQ("INFINITY", _gc(xdtoaf(_atanhf(+1))));
  EXPECT_STREQ("-INFINITY", _gc(xdtoaf(_atanhf(-1))));
  EXPECT_TRUE(isnan(_atanhf(+1.1)));
  EXPECT_TRUE(isnan(_atanhf(-1.1)));
}

BENCH(_atanh, bench) {
  volatile double a = .5;
  EZBENCH2("atanhf", donothing, EXPROPRIATE(_atanhf(a)));
  EZBENCH2("atanh", donothing, EXPROPRIATE(_atanh(a)));
  EZBENCH2("atanhl", donothing, EXPROPRIATE(_atanhl(a)));
}
