/*-*- 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.internal.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"

TEST(log2l, test) {
  EXPECT_STREQ("1", gc(xdtoal(log2l(2))));
  EXPECT_STREQ("NAN", gc(xdtoal(log2l(NAN))));
  EXPECT_STREQ("0", gc(xdtoal(log2l(1))));
  EXPECT_STREQ("INFINITY", gc(xdtoal(log2l(INFINITY))));
  EXPECT_STREQ("-INFINITY", gc(xdtoal(log2l(0))));
  EXPECT_STREQ("-INFINITY", gc(xdtoal(log2l(-0.))));
  EXPECT_TRUE(isnan(log2l(-1)));
  EXPECT_TRUE(isnan(log2l(-2)));
}

TEST(log2, test) {
  EXPECT_STREQ("1", gc(xdtoa(log2(2))));
  EXPECT_STREQ("2", gc(xdtoa(log2(2 * 2))));
  EXPECT_STREQ("NAN", gc(xdtoa(log2(NAN))));
  EXPECT_STREQ("0", gc(xdtoa(log2(1))));
  EXPECT_STREQ("INFINITY", gc(xdtoa(log2(INFINITY))));
  EXPECT_STREQ("-INFINITY", gc(xdtoa(log2(0))));
  EXPECT_STREQ("-INFINITY", gc(xdtoa(log2(-0.))));
  EXPECT_TRUE(isnan(log2(-1)));
  EXPECT_TRUE(isnan(log2(-2)));
}

TEST(log2f, test) {
  EXPECT_STREQ("1", gc(xdtoaf(log2f(2))));
  EXPECT_STREQ("NAN", gc(xdtoaf(log2f(NAN))));
  EXPECT_STREQ("0", gc(xdtoaf(log2f(1))));
  EXPECT_STREQ("INFINITY", gc(xdtoaf(log2f(INFINITY))));
  EXPECT_STREQ("-INFINITY", gc(xdtoaf(log2f(0))));
  EXPECT_STREQ("-INFINITY", gc(xdtoaf(log2f(-0.))));
  EXPECT_TRUE(isnan(log2f(-1)));
  EXPECT_TRUE(isnan(log2f(-2)));
}

BENCH(log2, bench) {
  double _log2(double) asm("log2");
  float _log2f(float) asm("log2f");
  long double _log2l(long double) asm("log2l");
  EZBENCH2("log2", donothing, _log2(.7));    //  ~8ns
  EZBENCH2("log2f", donothing, _log2f(.7));  //  ~6ns
  EZBENCH2("log2l", donothing, _log2l(.7));  // ~21ns
}
