<commit_before>// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/jxl/splines.h"

#include <algorithm>
#include <cmath>

#include "lib/jxl/ans_params.h"
#include "lib/jxl/base/status.h"
#include "lib/jxl/chroma_from_luma.h"
#include "lib/jxl/common.h"
#include "lib/jxl/dct_scales.h"
#include "lib/jxl/entropy_coder.h"
#include "lib/jxl/opsin_params.h"

#undef HWY_TARGET_INCLUDE
#define HWY_TARGET_INCLUDE "lib/jxl/splines.cc"
#include <hwy/foreach_target.h>
#include <hwy/highway.h>

#include "lib/jxl/fast_math-inl.h"
HWY_BEFORE_NAMESPACE();
namespace jxl {
namespace HWY_NAMESPACE {
namespace {

// Given a set of DCT coefficients, this returns the result of performing cosine
// interpolation on the original samples.
float ContinuousIDCT(const float dct[32], float t) {
  // We compute here the DCT-3 of the `dct` vector, rescaled by a factor of
  // sqrt(32). This is such that an input vector vector {x, 0, ..., 0} produces
  // a constant result of x. dct[0] was scaled in Dequantize() to allow uniform
  // treatment of all the coefficients.
  constexpr float kMultipliers[32] = {
      kPi / 32 * 0,  kPi / 32 * 1,  kPi / 32 * 2,  kPi / 32 * 3,  kPi / 32 * 4,
      kPi / 32 * 5,  kPi / 32 * 6,  kPi / 32 * 7,  kPi / 32 * 8,  kPi / 32 * 9,
      kPi / 32 * 10, kPi / 32 * 11, kPi / 32 * 12, kPi / 32 * 13, kPi / 32 * 14,
      kPi / 32 * 15, kPi / 32 * 16, kPi / 32 * 17, kPi / 32 * 18, kPi / 32 * 19,
      kPi / 32 * 20, kPi / 32 * 21, kPi / 32 * 22, kPi / 32 * 23, kPi / 32 * 24,
      kPi / 32 * 25, kPi / 32 * 26, kPi / 32 * 27, kPi / 32 * 28, kPi / 32 * 29,
      kPi / 32 * 30, kPi / 32 * 31,
  };
  HWY_CAPPED(float, 32) df;
  auto result = Zero(df);
  const auto tandhalf = Set(df, t + 0.5f);
  for (int i = 0; i < 32; i += Lanes(df)) {
    auto cos_arg = LoadU(df, kMultipliers + i) * tandhalf;
    auto cos = FastCosf(df, cos_arg);
    auto local_res = LoadU(df, dct + i) * cos;
    result = MulAdd(Set(df, square_root<2>::value), local_res, result);
  }
  return GetLane(SumOfLanes(result));
}

// Splats a single Gaussian on the image.
void DrawGaussian(Image3F* const opsin, const Rect& opsin_rect,
                  const Rect& image_rect, const Spline::Point& center,
                  const float intensity, const float color[3],
                  const float sigma, std::vector<int32_t>& xs,
                  std::vector<int32_t>& ys,
                  std::vector<float>& local_intensity_storage) {
  // Sanity check sigma, inverse sigma and intensity
  if (!(std::isfinite(sigma) && std::isfinite(1.0f / sigma) &&
      std::isfinite(intensity))) {
    return;
  }
  constexpr float kDistanceMultiplier = 4.605170185988091f;  // -2 * log(0.1)
  // Distance beyond which exp(-d^2 / (2 * sigma^2)) drops below 0.1.
  const float maximum_distance = sigma * sigma * kDistanceMultiplier;
  const auto xbegin_s =
      std::max<ssize_t>(image_rect.x0(), center.x - maximum_distance + .5f);
  const auto xend_s =
      std::min<ssize_t>(center.x + maximum_distance + .5f,
                        image_rect.x0() + image_rect.xsize() - 1);
  const auto ybegin_s =
      std::max<ssize_t>(image_rect.y0(), center.y - maximum_distance + .5f);
  const auto yend_s =
      std::min<ssize_t>(center.y + maximum_distance + .5f,
                        image_rect.y0() + image_rect.ysize() - 1);
  if ((xend_s) <= 0 || (xend_s < xbegin_s)) return;
  const size_t xbegin = xbegin_s;<commit_msg>Slight reformatting<commit_after>// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/jxl/splines.h"

#include <algorithm>
#include <cmath>

#include "lib/jxl/ans_params.h"
#include "lib/jxl/base/status.h"
#include "lib/jxl/chroma_from_luma.h"
#include "lib/jxl/common.h"
#include "lib/jxl/dct_scales.h"
#include "lib/jxl/entropy_coder.h"
#include "lib/jxl/opsin_params.h"

#undef HWY_TARGET_INCLUDE
#define HWY_TARGET_INCLUDE "lib/jxl/splines.cc"
#include <hwy/foreach_target.h>
#include <hwy/highway.h>

#include "lib/jxl/fast_math-inl.h"
HWY_BEFORE_NAMESPACE();
namespace jxl {
namespace HWY_NAMESPACE {
namespace {

// Given a set of DCT coefficients, this returns the result of performing cosine
// interpolation on the original samples.
float ContinuousIDCT(const float dct[32], float t) {
  // We compute here the DCT-3 of the `dct` vector, rescaled by a factor of
  // sqrt(32). This is such that an input vector vector {x, 0, ..., 0} produces
  // a constant result of x. dct[0] was scaled in Dequantize() to allow uniform
  // treatment of all the coefficients.
  constexpr float kMultipliers[32] = {
      kPi / 32 * 0,  kPi / 32 * 1,  kPi / 32 * 2,  kPi / 32 * 3,  kPi / 32 * 4,
      kPi / 32 * 5,  kPi / 32 * 6,  kPi / 32 * 7,  kPi / 32 * 8,  kPi / 32 * 9,
      kPi / 32 * 10, kPi / 32 * 11, kPi / 32 * 12, kPi / 32 * 13, kPi / 32 * 14,
      kPi / 32 * 15, kPi / 32 * 16, kPi / 32 * 17, kPi / 32 * 18, kPi / 32 * 19,
      kPi / 32 * 20, kPi / 32 * 21, kPi / 32 * 22, kPi / 32 * 23, kPi / 32 * 24,
      kPi / 32 * 25, kPi / 32 * 26, kPi / 32 * 27, kPi / 32 * 28, kPi / 32 * 29,
      kPi / 32 * 30, kPi / 32 * 31,
  };
  HWY_CAPPED(float, 32) df;
  auto result = Zero(df);
  const auto tandhalf = Set(df, t + 0.5f);
  for (int i = 0; i < 32; i += Lanes(df)) {
    auto cos_arg = LoadU(df, kMultipliers + i) * tandhalf;
    auto cos = FastCosf(df, cos_arg);
    auto local_res = LoadU(df, dct + i) * cos;
    result = MulAdd(Set(df, square_root<2>::value), local_res, result);
  }
  return GetLane(SumOfLanes(result));
}

// Splats a single Gaussian on the image.
void DrawGaussian(Image3F* const opsin, const Rect& opsin_rect,
                  const Rect& image_rect, const Spline::Point& center,
                  const float intensity, const float color[3],
                  const float sigma, std::vector<int32_t>& xs,
                  std::vector<int32_t>& ys,
                  std::vector<float>& local_intensity_storage) {
  // Sanity check sigma, inverse sigma and intensity
  if (!(std::isfinite(sigma) && std::isfinite(1.0f / sigma) &&
        std::isfinite(intensity))) {
    return;
  }
  constexpr float kDistanceMultiplier = 4.605170185988091f;  // -2 * log(0.1)
  // Distance beyond which exp(-d^2 / (2 * sigma^2)) drops below 0.1.
  const float maximum_distance = sigma * sigma * kDistanceMultiplier;
  const auto xbegin_s =
      std::max<ssize_t>(image_rect.x0(), center.x - maximum_distance + .5f);
  const auto xend_s =
      std::min<ssize_t>(center.x + maximum_distance + .5f,
                        image_rect.x0() + image_rect.xsize() - 1);
  const auto ybegin_s =
      std::max<ssize_t>(image_rect.y0(), center.y - maximum_distance + .5f);
  const auto yend_s =
      std::min<ssize_t>(center.y + maximum_distance + .5f,
                        image_rect.y0() + image_rect.ysize() - 1);
  if ((xend_s) <= 0 || (xend_s < xbegin_s)) return;
  const size_t xbegin = xbegin_s;
{'fraction_non_alphanumeric': 0.13851351351351351, 'fraction_numerical': 0.05039414414414414, 'mean_word_length': 2.7160041841004183, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2803234', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2623, 'n_words': 778}