Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
tea.h
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2020 Hubert Kowalski.
4 Copyright (C) 2020 Ralf Brown.
5 Copyright (C) 2022 Martin Bařinka.
6 Copyright (C) 2024 Alynx Zhou.
7 Copyright (C) 2026 Aurélien PIERRE.
8
9 darktable is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 darktable is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with darktable. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#pragma once
24
25// Allocate a buffer for storing the internal state of 'numthreads' parallel instances of the Tiny Encryption
26// Algorithm. We need to ensure that each state falls in a separate cache line, or all threads sharing a
27// cache line will be running in lock-step as the cache line bounces back and forth between them, effectively
28// cutting throughput by a factor equal to the number of threads sharing a cache line (8 with a 64-byte cache
29// line and 32-bit ints)
30#define TEA_STATE_SIZE (MAX(DT_CACHELINE_BYTES, 2*sizeof(unsigned int)))
31static inline unsigned int* alloc_tea_states(size_t numthreads)
32{
33 unsigned int* states = dt_pixelpipe_cache_alloc_align_cache(numthreads * TEA_STATE_SIZE, 0);
34 if (states) memset(states, 0, numthreads * TEA_STATE_SIZE);
35 return states;
36}
37
38// retrieve the state for the instance in the given thread from the array of states previously allocated with
39// alloc_tea_states()
40static inline unsigned int* get_tea_state(unsigned int* const states, int threadnum)
41{
42 return states + threadnum * TEA_STATE_SIZE/sizeof(states[0]);
43}
44
45static inline void free_tea_states(unsigned int* states)
46{
48}
49
50// How many rounds of the mixing function to run for one encryption
51#define TEA_ROUNDS 8
52
53// Run the encryption mixing function using and updating the given internal state. For use as a PRNG, you can
54// set arg[0] to the random-number seed, then read out the value of arg[0] after each call to this function.
55static inline void encrypt_tea(unsigned int *arg)
56{
57 const unsigned int key[] = { 0xa341316c, 0xc8013ea4, 0xad90777d, 0x7e95761e };
58 unsigned int v0 = arg[0], v1 = arg[1];
59 unsigned int sum = 0;
60 unsigned int delta = 0x9e3779b9;
61 for(int i = 0; i < TEA_ROUNDS; i++)
62 {
63 sum += delta;
64 v0 += ((v1 << 4) + key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1]);
65 v1 += ((v0 << 4) + key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + key[3]);
66 }
67 arg[0] = v0;
68 arg[1] = v1;
69}
70
71static inline float tpdf(unsigned int urandom)
72{
73 float frandom = (float)urandom / (float)0xFFFFFFFFu;
74
75 return (frandom < 0.5f ? (sqrtf(2.0f * frandom) - 1.0f) : (1.0f - sqrtf(2.0f * (1.0f - frandom))));
76}
77
78
79// clang-format off
80// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
81// vim: shiftwidth=2 expandtab tabstop=2 cindent
82// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
83// clang-format on
const float i
Definition colorspaces_inline_conversions.h:669
const float delta
Definition colorspaces_inline_conversions.h:722
char * key
Definition common/metadata.c:60
#define dt_pixelpipe_cache_alloc_align_cache(size, id)
Definition darktable.h:357
#define dt_pixelpipe_cache_free_align(mem)
Definition darktable.h:377
static float tpdf(unsigned int urandom)
Definition tea.h:71
#define TEA_ROUNDS
Definition tea.h:51
static unsigned int * get_tea_state(unsigned int *const states, int threadnum)
Definition tea.h:40
static void free_tea_states(unsigned int *states)
Definition tea.h:45
#define TEA_STATE_SIZE
Definition tea.h:30
static unsigned int * alloc_tea_states(size_t numthreads)
Definition tea.h:31
static void encrypt_tea(unsigned int *arg)
Definition tea.h:55