/*-*- 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/assert.h"
#include "libc/calls/calls.h"
#include "libc/intrin/kprintf.h"
#include "libc/limits.h"
#include "libc/log/countexpr.h"
#include "libc/macros.internal.h"
#include "libc/mem/alg.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"

#ifdef __x86_64__

static long GetLongSum(const long *h, size_t n) {
  long t;
  size_t i;
  for (t = i = 0; i < n; ++i) {
    if (__builtin_add_overflow(t, h[i], &t)) {
      t = LONG_MAX;
      break;
    }
  }
  return t;
}

static size_t GetRowCount(const long *h, size_t n) {
  while (n && !h[n - 1]) --n;
  return n;
}

static void PrintHistogram(const long *h, size_t n, long t) {
  size_t i;
  long j, p;
  char s[101];
  unsigned long logos;
  for (i = 0; i < n; ++i) {
    p = (h[i] * 10000 + (t >> 1)) / t;
    _unassert(0 <= p && p <= 10000);
    if (p) {
      for (j = 0; j < p / 100; ++j) s[j] = '#';
      s[j] = 0;
      logos = i ? 1ul << (i - 1) : 0;
      kprintf("%'12lu %'16ld %3d.%02d%% %s\n", logos, h[i], p / 100, p % 100,
              s);
    }
  }
}

void countexpr_report(void) {
  long hits;
  struct countexpr *p;
  for (p = countexpr_data; p->line; ++p) {
    if ((hits = GetLongSum(p->logos, 64))) {
      kprintf("%s:%d: %s(%s) %'ld hits\n", p->file, p->line, p->macro, p->code,
              hits);
      PrintHistogram(p->logos, 64, hits);
    }
  }
}

static textstartup void countexpr_init() {
  atexit(countexpr_report);
}

const void *const countexpr_ctor[] initarray = {
    countexpr_init,
};

#endif /* __x86_64__ */
